local RunScript = coroutine.wrap(function(PlayerMovment) for i , Player in pairs(game.Players:GetChildren())do if(Player:FindFirstChild("PlayerGui"):FindFirstChild("Check")~=nil) then PlayerMovment:Clone().Parent = Player:FindFirstChild("Backpack") Player:FindFirstChild("Backpack"):FindFirstChild("PlayerMovments").Disabled = false wait() end end coroutine.yield() -- makes the in suspend mode ( this is how i can loop it ) end)
i am trying to use coroutine . i am not really expert on them . so .. i have a while loop with a lot of functions a specific function i use call the coroutine and use it and then keep going on to the next function .
after the function finish the while loop start again and when it get to the coroutine i get an error on the output that the coroutine is dead. now i read the article and i do know what that means but i tried to yield it but it didnt work .
can someone show me what did i do wrong ? it will really help me . Thanks
coroutine.yield
acts a bit like a wait
command, except that it never resumes automatically, only when you call coroutine.resume
on the yielded thread. However, once the function is done executing, you cannot resume it - as the error you got says, the coroutine is dead.
You have a couple options:
Option #1
coroutine.resume(coroutine.create(function() while true do wait() for i , Player in pairs(game.Players:GetChildren())do --etc end end end))
Option #2
local PlayerMovment = _____________ --complete this line game.Players.ChildAdded:connect(function(player) local playerGui = player:WaitForChild("PlayerGui") local backpack = player:WaitForChild("Backpack") playerGui.ChildAdded:connect(function(child) if child.Name == "Check" then spawn(function() child:Destroy() end) --needs to be in a separate coroutine or else Roblox complains that we're trying to remove 'child' before it's finished adding it local obj = PlayerMovment:Clone() obj.Parent = backpack obj.Disabled = false end end) end)
Here is my function and its coroutine
local RunScript = coroutine.wrap(function(PlayerMovment) for i , Player in pairs(game.Players:GetChildren())do if(Player:FindFirstChild("PlayerGui"):FindFirstChild("Check")~=nil) then PlayerMovment:Clone().Parent = Player:FindFirstChild("Backpack") Player:FindFirstChild("Backpack"):FindFirstChild("PlayerMovments").Disabled = false wait() end end end) function GetReadyPlayers(MapNumber) local Map = game.Workspace:FindFirstChild("Map"..MapNumber) -- the player is in this map local PlayerMovment = Map:FindFirstChild("PlayerMovments") -- this moves the player to a point game.Workspace.RaceMessage.Value = true -- this tells the local scripts to start working ( Check RaceMessage Gui ) wait(2.9) RunScript(PlayerMovment) -- this gives all the players specific script AT THE SAME TIME !! COOOL --- now we set everything back to normal -- if(game.Workspace.RaceMessage.Count.Value == game.Workspace.PlayersWantPlay) then -- this means that all the players saw the 3 2 1 go message game.Workspace.RaceMessage.Value = false -- off this for i ,j in pairs(game.Players:GetChildren())do if(j:FindFirstChild("PlayerGui"):FindFirstChild("Check")~=nil) then local LocalScript = j:FindFirstChild("PlayerGui"):FindFirstChild("RaceMessage"):FindFirstChild("Texter"):FindFirstChild("LocalScript") LocalScript.Disabled = true wait(0.5) LocalScript.Disabled = false -- now the repeat wait() starts again till the next round wait() end end end Set_Winners(Map) -- another functions RefreshAndFix(Map) wait(1) Give_GuiEnding() end