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

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

Asked by
F_lipe 135
6 years ago

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)
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. RayCurse 1518 — 6y
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 F_lipe 135 — 6y

1 answer

Log in to vote
1
Answered by
Validark 1580 Snack Break Moderation Voter
6 years ago

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.

Ad

Answer this question