2

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.

2
  • 3
    In regular lua you would use dofile() which runs the script every time. require() will only load the module once, but it could return a function that you can call whenever when you want. In roblox maybe this? Commented Aug 25, 2018 at 18:29
  • 1
    In pure LUA there is a similar question with a detailed answer on this topic: stackoverflow.com/questions/14205157/… Commented Aug 27, 2018 at 12:03

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.