lua custom terminal not having command outputs - lua

Im trying to make a terminal but im stuck on one thing. In the doer program command do. I want docom to be the output of of the loadstring. input = io.read() its a lua terminal inside my program but nothing displays any output. Here is the code that is relevant:
docom = loadstring(input)
print(docom)
How do i make the output display? Because currently its like this:
welcome to the terminal!
loaded
do
do:
print("hello")
function: 0x809b60
do:
The third and fifth line are user inputs. how do i fix this so it shows the hello string instead of the function name. i want this to be able to manage it as i have everything else in the same lua script. please help.

You probably want print(docom()).
loadstring compile a script into a function. That's what you see function: 0x809b60.
loadstring does not run the function. Hence the call docom().
You may want to add error handling by checking whether docom is nil and by calling docom via pcall.

Related

SciTE can not find user defined function. The editor executes "/bin/sh" for some reason

So, this is my first attempt to make my source code editing more comfortable. I follow the SciTE scripting guide.
ext.lua.startup.script=$(SciteDefaultHome)/startup.lua
command.name.1.*= Programmer's Manual (selected text)
command.subsystem.1.*= 3
command.1.*=man_select
command.shortcut.1.*=F1
Above is the user properties file. It binds F1 key to user defined Lua function.
The problem is, that insteed of starting the function, man_select in my case, the SciTE editor output gives me
>man_select
/bin/sh: man_select: not found
>Exit code: 127
So, the editor runs a shell with my function name as a argument. I can't find no SciTE logs, or console, to see what the cause is.
I define man_select as a simple Lua function, without arguments:
function man_select()
local sel = editor:GetSelText()
...
end
What should I do to make my own Lua functions visible to SciTE editor? Is there a way to ask SciTE for more information on its scripting system flow?

{LUA} How to fire another script in script?

I have a question from Lua/Roblox!
Basically, I want to fire a script from a script. This may sound like a stupid question, but actually it isn't :P
For example:
I have a script: script1 in ServerScriptStorage.
And, I want to code it to fire contents of script2.
Examples:
Content of script1:
game.Players.PlayerAdded:Connect(function()
HERE SCRIPT2 FIRING!
end)
Content of script2:
print("This message is triggered by event in script!")
This is fairly simple task I suppose, so please give me the SIMPLEST and SHORTEST version of code. I don't need any exclusives such like launching 2 script in 1. I'm a begginer script, so please keep it simple.
Thanks, NorteX.
In pure Lua, using dofile would probably make the most sense. However, in Roblox, the approach must be much different. The way I would recommend doing this is using a ModuleScript for "Script2". Then you would load the script using require(). Because "requiring" a script caches the returned value for future "requires", this means that the contents of the ModuleScript will only be executed once. Thus, if you have code you want to run multiple times, you should encapsulate it in a function that the ModuleScript returns.
Here's how the code would look like given your setup:
Script1:
local script2 = require(game.ServerScriptService.Script2)
game.Players.PlayerAdded:Connect(function(player)
script2()
end)
Script2:
-- In game.ServerScriptService.Script2 as a ModuleScript
return function()
print("This message is triggered by event in script!")
end
Check out the documentation for ModuleScripts to understand more about them.
workspace.SCRIPT2.Disabled = true -- Disables SCRIPT2 , you can remove this and manually disable it in SCRIPT2's properties.
game.Players.PlayerAdded:Connect(function()
workspace.SCRIPT2.Disabled = false -- Activates SCRIPT2. You can alter the "disabled" state multiple time to make it reboot and operate more than once.
end)
Also, you could replace workspace.SCRIPT2.Disabled by the location where your second script is, by example, workspace.FolderOne.scripts.SCRIPT2.Disabled . Just be sure it points to the script and keeps the "disabled" part on, so it knows to disable / enable it.

How to call a function from moonscript in lua?

I've got a moon script code like this:
hello = (name) ->
print "Hello #{name}!"
And i want to use it in my lua code using moonscript.loadfile
how should i do something like that?
MoonScript code compiles to Lua, so the function you've written is actually a Lua function when it's being executed.
There are a few ways to get access to it in Lua:
Compile the file ahead of time using the moonc command line tool. This will give you a a .lua file that you can load as you would any other Lua file.
Load the file using one of the MoonScript loader function. moonscript.loadfile is a lower level function, and I don't recommend using it unless that's what you specifically need. The easiest way is to call require "moonscript" in your program, then Lua's require function is augmented to be able to load MoonScript files directly. There's more information on the Compiler API reference page.
Keep in mind that if you have function in another file, you need to export them as part of the module. You do this by having a return value for the module. The typically pattern is to return a table that contains all the function you would want to use. In MoonScript, the last line in a file is automatically converted into a return statement. Assignment is no coerced into a return though, so I recommend structuring your module like this:
hello = (name) ->
print "Hello #{name}!"
{:hello}

An error with io.read() in lua

When I use io.read() I get input from the user but when I do so it wont count backspaces, so if I type:
blah blah blaht
when my program wants input then if I delete the t:
blah blah blah
It still reads it as if the t is there, help?
EDIT: Only does this when debugging, im using SciTE, thx
I have been trying to figure this out for a long time and I came across this:
You have two programs here: SciTE and cmd. SciTE sends each visible
character like 'r' through to cmd as soon as it is typed. ScITE
interprets the backspace key internally to change the buffer but does
not send the key through to cmd. You can not edit a command line as
you want. Simplest to type Enter and try again. (source)
Unfortunately it doesn't seem to be possible to fix this.
Your best options are:
Execute the Lua script outside of SciTE and then you wont have this issue
Use another editor

How to import lua file to execute it?

I'm using Lua Mac 5.1.4 Compiler.
I'm trying to import lua file and run it.
I tried using this code:
% lua hello.lua
But I'm getting this error: stdin:1: unexpected symbol near '%'
Am I doing something wrong? This is my first day using lua so be easy on me.
Thank you.
The problem is probably that you saw this verbatim text in a tutorial:
% lua hello.lua
The '%' at the beginning of the line is not something you are supposed to type into your terminal, but is rather a generic prompt indicator. Sometimes you might see it written as '$' instead:
$ lua hello.lua
In either case, the first character is not something you type, but rather is a typographical convention to suggest that what follows is to be typed at a prompt. Your actual prompt might look something like this:
mo#macbook$
So you would type lua hello.lua but your screen would look like this:
mo#macbook$ lua hello.lua
So, try just entering lua hello.lua and see what happens.
Note that the error message you got regarding stdin:1 is likely from your shell (e.g. bash), and not from Lua (which never even started running due to the malformed command in the shell).
The error stdin:1: unexpected symbol near '%' suggests that you typed in % lua hello.lua while in an interactive lua session (or you executed a script containing it). Now that's something that you should type in in the commandline window.
Instead try something like print'Hello World!'
Lua provides two ways to call a file. One is the loadfile() and the other is the dofile() commands. Try using dofile("hello.lua").That should work.If it doesn't type the absolute path...:)

Resources