Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How come this script can run several options with several yields?

So I wrote this yesterday and while at school I realized that inside each function I wait 8 minutes and 12 minutes respectively. Why can the other function in the script run while the opposite function is on the wait time I thought this would occupy the thread?

It's not an error or anything; the script works as planned. I'm just worried it might mess up and error at some point during gameplay

local blueCrystal = game.Workspace:WaitForChild("Blue_Crystal")
local blueClickDetector = blueCrystal.ClickDetector
local isBlueSpawned = true

local redCrystal = game.Workspace:WaitForChild("Red_Crystal")
local redClickDetector = redCrystal.ClickDetector
local isRedSpawned = true


--Blue Code
function onBlueClicked(player)
    if isBlueSpawned == true then
        isBlueSpawned = false
        local playerHumanoid = player.Character.Humanoid
        playerHumanoid.WalkSpeed = 40
        for i = 0, 1, .1 do
            wait()
            blueCrystal.Transparency = i
        end
        blueCrystal:Destroy()
        wait(480)
        local newCrystal = game.ReplicatedStorage.Blue_Crystal:Clone()
        newCrystal.Parent = game.Workspace
        blueCrystal = newCrystal
        blueClickDetector = newCrystal.ClickDetector
        blueClickDetector.MouseClick:Connect(onBlueClicked)
        isBlueSpawned = true
    end
end

--Red Code
function onRedClicked(player)
    if isRedSpawned == true then
		isRedSpawned = false
        local sniper = game.ReplicatedStorage["sniper gang"]:clone()
		sniper.Parent = player.Backpack
        for i = 0, 1, .1 do
            wait()
            redCrystal.Transparency = i
        end
        redCrystal:Destroy()
        wait(720)
        local newCrystal = game.ReplicatedStorage.Red_Crystal:Clone()
        newCrystal.Parent = game.Workspace
        redCrystal = newCrystal
        redClickDetector = newCrystal.ClickDetector
        redClickDetector.MouseClick:Connect(onRedClicked)
        isRedSpawned = true
    end
end

--Black Code

redClickDetector.MouseClick:Connect(onRedClicked)
blueClickDetector.MouseClick:Connect(onBlueClicked)

2 comments

1

The two functions will each get their own thread which means they can run simultaneously. If this is not intended behavior, add in a boolean value to keep of the state across both functions.

0

Thanks, that's simple enough to understand. It seems my definition of what a thread is was a little bit skewed I was under the impression every script is assigned a thread to run its code

1 answer

✓ Accepted answer

Every Event fires each of its connected functions on new threads. This can be simulated with BindableEvents. You can also run a function on a separate thread directly using coroutines, or the spawn function, or the delay function.

This discussion is preserved as it was. New answers, comments, and votes are closed.