Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How to restart/redo a script after the code in it has been executed???

Asked by
Galicate 106
6 years ago

So how would I redo a script after all the code in it has been executed.

Example: print("wow")

But without doing while true do or for or repeat.

0
You can put the code in a function. You can also put the code in a module script as well. RayCurse 1518 — 6y

2 answers

Log in to vote
0
Answered by 6 years ago

The only way i would recommend is putting it in a function and/or putting it in loops. If you wanna get really dangerous, then you could make a script that creates a script that prints that out. Script-ception

Ad
Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

Lets me just tell you, its much more effective to use loops. However, there are a couple of ways how to achieve this:

Number 1:

Make it a module script. A module script doesn't run on its own when a game starts, but it executes itself when called with a require() function. For example, if you have a module script in the workspace called "thisIsAScript", you can call it by doing:

require(workspace.thisIsAScript)

However, module scripts are designed to be run only once per existence. This means that once you call it once, it will do nothing if you try calling it again. To get past this, you would need to make a copy of the script every time you are going to use it using the Clone() function, then remove it when the script ended. Your module script would look like this:

local module = {}
script:Clone().Parent = script.Parent

print("wow")

return module

and every time you want to call it you will need to do this:

require(workspace.ModuleScript) --Whatever your script is
workspace.ModuleScript:Destroy()

Number 2:

A more effective way to achieve what you want is by using _G. functions. To define such function, you need to do:

_G.functionNameGoesHere = function(whateverParametersYouNeedGoHere)
    --Code
end

_G.functions are basically normal functions, but you can call them from anywhere (any script in your place). If you chose this solution, you would have a Script (Inside ServerScriptService) with the following code:

_G.printWow = function()
    print("wow")
end

And to call it:

_G.printWow()

Number 3:

There is one more way, but this way is the least effective. Make a script with the following code, and make sure that its Disabled property is set to true. Name the script "PrintWowScript" and place it in the workspace.

print("wow")
script:Destroy()

And then if you want to call it:

local scr = workspace.PrintWowScript:Clone()
scr.Parent = workspace
scr.Disabled = false

I'll recommend the 2nd way using _G. functions, but its up to you.

Also, accept this if this helped. thx :D

Answer this question