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

coroutine error ? whats wrong ?

Asked by 8 years ago
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

0
error? GullibleChapV2 155 — 8y

2 answers

Log in to vote
0
Answered by 8 years ago

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:

  1. Spawn threads with infinite loops
  2. Use events (like ChildAdded) to determine when to do something (ex listen for Backpack.ChildAdded; if the child's name is "Check" then add your "PlayerMovment" to their backpack)
  3. Make a new coroutine whenever you want to call the function
  4. If you can remove the 'wait' command from your function(s), you can just call them without coroutines.

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)
0
@chess , i'll describe my problem although i did understand your answer. i have a Main While Loop that starts like a game ( like a minigame ) now the while loop will finish at the end and then it will come back to the coroutine and cant access it since its dead . its just complicated like why cant it start working like the other functions ? temprezors 0 — 8y
Ad
Log in to vote
0
Answered by 8 years ago

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
0
the code above will run always but the coroutine make it imposible . the racemessage appear at the same time the PlayerMovments script startworking and thats why i need a coroutine temprezors 0 — 8y

Answer this question