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

What is wrong with this script?(debounces and coroutines)

Asked by 10 years ago

I am trying to make a function that moves all the players once an event happens, the function worked fine until I put a debounce in, then suddenly 2-3 players from the table would not be moved. I believe this is due to my use of coroutines, can think of reasons why this is happening?

jailed is a table of players, the players are meant to have the for loop done to them (remove/change their table, move their characters etc) but some of the people in the jailed table are left out as soon as I added in the debounce.

function jailbreak(player)
    if jbdb == true then
        jbdb = false
        lastman = nil
        someoneDied = false
        game.Lighting.FogEnd = 100000000
        numJb = numJb + 1
        for i,v in pairs(criminals) do
            if v.Character and v.Character.Humanoid and v.Character.Humanoid.Health > 0 and v.Character.Head and (unlock.Position - v.Character.Head.Position).magnitude < 80 then
                v.Character:MoveTo(currentMap.CopSpawns:GetChildren()[math.random(1,6)].Position)
            end
        end
        for i,v in pairs(jailed) do
            stopSpec:FireClient(v)
            if v.Character and v.Character.Torso and v.Character.Humanoid and v.Character.Humanoid.Health > 0 then
                makeCriminal(v)
                wait()
                v.Character:MoveTo(currentMap.CopSpawns:GetChildren()[math.random(1,6)].Position)
                local CopNum = v:FindFirstChild("CopNum")
                CopNum.Value = defaultJail
                if v.Character.Torso then
                    local spar = Instance.new("Sparkles",v.Character.Torso)
                    coroutine.wrap(function()
                        wait(8)
                        if v and v.Character and v.Character.Humanoid then
                            v.Character.Humanoid.WalkSpeed = 16
                            spar:remove()
                        end 
                    end)()
                end
                v.Character.Humanoid.WalkSpeed = 40
            end     
        end
        for i,v in pairs(criminals) do
            Restrictions:FireClient(v,false,Restrs)
        end
        if player and player.Character and player.Character.Humanoid then
            player.Character:MoveTo(currentMap.CopSpawns:GetChildren()[math.random(1,6)].Position)
            local CopNum = player:FindFirstChild("CopNum")
            CopNum.Value = defaultJail
            local spar = Instance.new("Sparkles",player.Character)
            player.Character.Humanoid.WalkSpeed = 40
            coroutine.wrap(function()
                wait(8)
                player.Character.Humanoid.WalkSpeed = 16
                spar:remove()
            end)()
        end 
        Event:FireAllClients("Jailbreak")
        coroutine.wrap(function()
            wait(38)
            jbdb = true
            if someoneDied and #criminals > 0 and #jailed > 0 then
                for i,v in pairs(criminals) do
                    Restrictions:FireClient(v,"clear",Restrs)
                end 
                someoneDied = false
            else
                numJb = 0   
            end
        end)()
    end 
end
1
A warning: `and v.Character.Torso` will cause an error, not be false `false`, when there is no humanoid. Use `:FindFirstChild("Torso")` instead. BlueTaslem 18071 — 10y
0
Thanks. A small update: I added a print to the iteration of the jailed table so that every time it changes i,v it prints the contents of the table. I can confirm that the entire table is there but it cuts short crackabottle123 110 — 10y
0
Update: No matter how many people there are in the table it always works on all of them EXCEPT ONE (ie. 2 people and one moves, 6 people and 5 move) crackabottle123 110 — 10y
0
I fixed this by repeating the same loop 3 seconds later, but I'm still interested in the issue with this script (if there even is one, maybe im just stupid) crackabottle123 110 — 10y

1 answer

Log in to vote
0
Answered by 8 years ago

fix the loop.

Ad

Answer this question