YES this is a gmod and a lua question!
I would like to know if its possible to run LUA commands though an addon when the user has just launched the game and if so how?
It is not possible to run Lua commands in the menu state when the user has launched the game. This is unless you mean when they first spawn into a map, which is possible - and is what I'm about to describe.
You can use the hook system to hook onto events that happen in the game. One of these hooks is Initialize which is called when the game first loads and Lua is initialized. Another option is InitPostEntity which is called after all map entities have been spawned.
To use the hook system, call hook.Add("Hook name", "Custom identifier", function(...) end)
For example, to use the Initialize hook, use this code:
hook.Add("Initialize", "myidentifier", function()
-- put your code here
end)
Related
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.
I'm currently working on a lua program.
I want to use it in Minecraft with a mod called "OpenComputers" which allows the use of lua scripts on emulated systems.
The programm I'm working on is relatively simple: you have a console and you enter a command to control a machine.
It looks like this:
while(true) do
io.write("Enter command\n>")
cmd = io.read()
-- running code to process the command
end
But the problem is: I need a routine running in the background which checks data given by the machine.
while(true) do
-- checking and reacting
end
How can I make this work?
I can't jump to a coroutine while waiting on io.read()
It's not enough to check after someone used a command (sometimes I don't use it for days but I still have to keep an eye on it)
I'm relatively new to lua so please try to give a simple solution and - if possible - one that does not rely on third party tools.
Thank you :)
If you have some experience with opencomputers, you can add a(n asynchronous) listener for "key_down", and store the user input in a string (or whatever you want).
For example:
local userstr = ""
function keyPressed(event_name, player_uuid, ascii)
local c = string.char(ascii)
if c=='\n' then
print(userstr)
userstr = ""
else
userstr=userstr..c
end
--stores keys typed by user and prints them as a string when you press enter
end
event.register("key_down", keyPressed)
Running multiple tasks is a very broad problem solved by the operating system, not something as simple as Lua interpreter. It is solved on a level much deeper than io.read and deals with troubles numerous enough to fill a couple of books. For lua vm instead of physical computer, it may be simpler but it would still need delving deep into how the letters of code are turned into operations performed by the computer.
That mod of yours seems to already emulate os functionality for you: 1,2. I believe you'll be better off by making use of the provided functionality.
I was just wondering in roblox if anyone has ever come upon a situation where they needed to run scripts from another script. My situation is that I am making a control point system for a game. I need to be able to know if the other points are captured in order to capture the next ones so I am attempting to write a controller on top of it but i am not sure exactly how to access the functions from within the control point script.
Yes! ROBLOX provides a very useful feature called ModuleScripts for the very purpose of calling scripts from other scripts. The icon for ModuleScripts looks the same as the icon for regular Scripts except with a little brick in the bottom-right.
The way it works is that regular Scripts can call ModuleScripts in the game using the special require() function. The easiest way to explain this is with an example.
To begin, let's imagine that we have a Script and a ModuleScript. The Script will be located at game.Workspace.normalScript, and the ModuleScript will be located inside a brick called part at game.Workspace.part.moduleScript.
moduleScript will contain the following code:
script.Parent.Transparency = .5 --"Parent" is the part since this ModuleScript is located inside the part
Now, normalScript will contain the following code:
require(game.Workspace.part.moduleScript)
When you run the game, normalScript will execute moduleScript, changing the transparency of part to .5. When one calls require() on a ModuleScript, it will act as though it were a normal function being called. moduleScript acted as though it were a function, and ModuleScripts in general act the same way as functions for the most part.
This also means that ModuleScripts can return values like functions. For example, if we have the following code in moduleScript:
return 3+3
Now, our script will contain the following code:
local number = require(game.Workspace.moduleScript)
print(number) --> 6
This code will print "6" to the console since moduleScript returned 6. As you might guess, this means that ModuleScripts have many more uses than simply remotely executing code.
Here are two more examples of uses of ModuleScripts:
1) Returning functions:
moduleScript:
return function()
print("hey")
end
normalScript:
local func = require(game.Workspace.moduleScript)
func() --> hey
2) Returning modules such as apple below:
moduleScript:
local apple = {}
apple.flavor = "sweet"
return apple
normalScript:
local fruit = require(game.Workspace.moduleScript)
print(fruit.flavor) --> sweet
These are rather silly examples of ModuleScript uses, but ModuleScripts can actually be very powerful tools. For some cool examples, visit the ROBLOX Wiki page on ModuleScripts and scroll about halfway down.
I just want to call some specific function in my Lua script.
A simple script:
msg("hello")
function showamsgbox()
msg("123")
end
I just want to let my C app call showamsgbox() only but not to run msg("hello") beacuse it will show a msgbox when i load this script! So how to do that to keep this situation away?
PS:it is just example.sometimes i want to let users make thier own plugins in my program.but I do not want them write something outside the functions(i want to use functions to decide what to do.for example function OnLoad() means it will be run when i load it ).If there is something outside functions i cannot control them!
You can't. The script defines two variables when run: a and geta. Recall that function geta()...end is the same as geta=function()...end.
The a = 9 will be called when the script is initially evaluated in a lua_State.
If you reuse that lua_State instance, you can retrieve the function and invoke it without re-initializing a.
It seems that you want to sandbox scripts. Just give them a suitable, separate environment before running them. It may be an empty one or it may contain references to the functions you want them to use. They can write at will in their environment and it will not affect yours. Then just get the value of OnLoad or whatever user function you want to call and call it.
I'm writing a GUI that's meant to be easily customizable by the end-users. The functions are in C++ and are called from Lua. I'm trying to make a Sleep() type function that will pause the script but not the program itself.
I was able to get it working by using threads and making one for each function. However, I want it to be an individual function. As in, instead of having it part of the CreateButton function and every other function, simply having a Delay or Sleep function that only halts the script, not the entire program.
Me being a novice at Lua, I really don't know how to go about this. Any help is appreciated.
I'd look into making a state machine using coroutines and message passing. Treat each button push like a c++ string that gets passed into coroutine resume. You can then build a little state machine that switches on the message. You can then do some UI work and then put the coroutine back to sleep till something sends it another message.
This is pretty handy if you have a state machine that does UI.
pseudo code:
c_obj:wait_for_message("mouse_down");
local message = coroutine.yield();
if(message == "mouse_down") then
update draw function.
end
c_obj:wait_for_message("mouse_up");
local message = coroutine.yield();
if(message == "mouse_up") then
Update UI..
update draw function.
end
etc...
To make your busy-waiting solution more efficient, how about using select() or similar to wait for some GUI events to process, rather than spinning? It seems like something you would need to do in the GUI regardless of the scripting side of things.