How to create:
A script that creates a script with a script inside haha
How to create:
A script that creates a script with a script inside haha
You don't. Not possible unless there already is the script, stored somewhere and disabled, in that case, it can be cloned, parented, and reenabled but otherwise you can't create a new script or set its source during runtime.
Do you mean like making another script run in a script? Well, that's possible, through coroutines! Coroutines create another thread inside a script. Threads are like another scripts running at the same time as the script. For example, you want to print two words (for example, "foo" and "bar") at the same time, but you want to wait 5 seconds before printing "bar" together with "foo", well that is possible through coroutines! To do that, you create two loops: one for "foo" (normal loop) and one for "bar" (coroutine loop). This is how it looks:
local barCoroutine = coroutine.create(function()
task.wait(5) -- 5 second delay
while true do -- creates the loop
print("bar")
task.wait(1)
end
end)
coroutine.resume(barCoroutine) --plays the coroutine
while true do
print("foo")
task.wait(1)
end
What this script will do is it will print "foo" first, then after 5 seconds, it will start to print "bar" as well. Pretty cool!
There is another way to create and play coroutines without using coroutine.create() and coroutine.resume(). And that is by using coroutine.wrap(). That function creates a new coroutine as a function, so to play that coroutine you must put () at the end of it, like normal functions. So
local barCoroutine = coroutine.create(function()
task.wait(5)
while true do
print("bar")
task.wait(1)
end
end)
coroutine.resume(barCoroutine)
and
local barCoroutine = coroutine.wrap(function()
task.wait(5)
while true do
print("bar")
task.wait(1)
end
end)
barCoroutine()
is and does the same thing.
More documentation and explanation can be read from the wiki, the DevForum, and some YouTube tutorials.
Coroutines also do the same things as some built-in functions such as task.spawn(), task.defer(), and task.delay(), but coroutines have more accessibility and functions.
-- this coroutine
local barCoroutine = coroutine.create(function()
task.wait(5)
while true do
print("bar")
task.wait(1)
end
end)
coroutine.resume(barCoroutine)
-- does the same thing as
task.spawn(function()
task.wait(5)
while true do
print("bar")
task.wait(1)
end
end)
-- or
task.delay(5, function()
while true do
print("bar")
task.wait(1)
end
end
CONCLUSION: The reason why coroutines exist is to make another threads in a script that can be helpful in loops. You cannot have two loops in a script without coroutines because anything in a script that is written after a loop (more specifically while loops) cannot be run because the script cannot leave the loop unless it was broken. Coroutines are also improved and more accessible versions of the task library functions.
-- Script #1
while true do -- loop #1
print("foo")
task.wait(1)
end
while true do -- loop #2
print("bar") -- this won't print because the script is stuck on the first loop. if the first loop breaks, it won't run anymore and move on to the second loop
task.wait(1)
end
-- Script #2
local barCoroutine = coroutine.create(function()
task.wait(5)
while true do -- loop #2
print("bar") -- this can print the same time as loop #1 because loop #2 was made as a coroutine
task.wait(1)
end
end)
coroutine.resume(barCoroutine) --plays the coroutine
while true do -- loop #1
print("foo") -- this will print the same time as loop #2
task.wait(1)
end
This discussion is preserved as it was. New answers, comments, and votes are closed.
1 comment
alr ty!