Help with this function

So i have been recently going to toolbox and just randomly playing around and i saw quite a lot of functions that is something like

spawn(function()

end)

can someone pls explain it to me what is that also im begginer so pls make it simple when explaining

spawn is just like running another script in one script, so if you have a infinite while loop in that function, everything below will still run because it is seperated

can you give an example on how to use it?

spawn is basically a way to create a new script whilst keeping it in all 1 script

Say I have this:

while true do
    wait(1)
    print("1")
end

while true do
    wait(1)
    print("2")
end

Now here’s the thing: Only the first loop will start but the second one won’t :thinking: Why is that? Well, since both are while true do loops the script will keep running that first loop infinitely & won’t continue the second

Now here’s the cool part, we can use spawn to basically create a new entire like-script (Or thread) inside that same script while keeping our first loop running

local function Loop1()
    while true do
        wait(1)
        print("1")
    end
end

spawn(Loop1) --We want to call this function here 

while true do
    wait(1)
    print("2")
end

So now, we have 2 loops running at the same time!

2 Likes

thx now i understand it you explain it so perfectly