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

Breaking after one loop help?

Asked by
NotSoNorm 777 Moderation Voter
9 years ago

So it's supposed to add a string value to a player when they join, and remove when they die or leave, it's pretty much a guess the song game. There is no errors though

math.randomseed(tick())

colors = {
    {"Gray",0},
    {"Cyan",0},
    {"Brown",0},
    {"Green",0},
    {"Pink",0},
    {"Peach",0},
    {"Blue",0},
    {"Purple",0},
    {"Red",0},
    {"Yellow",0},
}
function win(name)
    local PointsService = game:GetService("PointsService")
    PointsService:AwardPoints(game.Players[name].userId, 50)
    game.Players[name].leaderstats.Wins.Value = game.Players[name].leaderstats.Wins.Value + 1
    game.Workspace.MusicScript.play.Value = false
    game.Workspace.MusicScript.CurrentSong.Value = ""
    game.ServerStorage.win:clone().Parent = game.Players[name].PlayerGui
    x = 0
    repeat x = x + 1 wait(1) until x == 15 or not game.Players:findFirstChild(name) or not game.Players[name].PlayerGui:findFirstChild("win")
    if game.Players:findFirstChild(name) then
        if game.Players[name].PlayerGui:findFirstChild("win") then
            game.Players[name].PlayerGui.win:Destroy()
            local num = math.random(1,#colors)
            game.Workspace.Colors.Color.Value = colors[num][1]
            game.Workspace.MusicScript.play.Value = true
        end
    else
        local num = math.random(1,#colors)
        game.Workspace.Colors.Color.Value = colors[num][1]
        game.Workspace.MusicScript.play.Value = true
    end
end

function spawnPlayer(child)
    spawned = false
    for i = 1,#colors do
        if colors[i][2] == 0 and spawned == false then
            spawned = true
            colors[i][2] = 1
            game.Players[child].Character.Torso.CFrame = game.Workspace.Colors[colors[i][1]]:findFirstChild(string.lower(colors[i][1]).."1").CFrame * CFrame.new(0,2,0)
            game.Players[child].Character.Humanoid.WalkSpeed = 0
        end
    end
    a = Instance.new("StringValue",script)
    a.Name = child
    a.Value = child
end

script.ChildAdded:connect(function(child)
    if game.Players:findFirstChild(child.Value) then
        spawnPlayer(child.Value)
        if #script:GetChildren() > 3 then
            game.Workspace.MusicScript.play.Value = true
        end
    end
end)

script.ChildRemoved:connect(function(child)
    if #script:GetChildren() <= 1 then
        if #script:GetChildren() == 1 then
            for j,k in pairs(script:GetChildren()) do
                game.Players[k.Name]:LoadCharacter()
                local PointsService = game:GetService("PointsService")
                PointsService:AwardPoints(game.Players[k.Name].userId, 150)
                k:Destroy()
            end
            for o,p in pairs(game.Workspace["Dead_Players"]:GetChildren()) do
                p:Destroy()
            end
            game.Workspace.rebuild.Value = true
            wait(15)
            for i,v in pairs(game.Players:GetChildren()) do
                spawnPlayer(v.Name)
            end
        else
            for o,p in pairs(game.Workspace["Dead_Players"]:GetChildren()) do
                p:Destroy()
            end
            game.Workspace.rebuild.Value = true
            wait(15)
            for i,v in pairs(game.Players:GetChildren()) do
                spawnPlayer(v.Name)
            end
        end
    end
end)

game.Players.PlayerAdded:connect(function(plr)
    repeat wait() until plr.Character ~= nil
    wait()
    if #script:GetChildren() < 10 then
        spawnPlayer(plr.Name)
    else
        print("wot")
    end
    plr.Character.Humanoid.Died:connect(function()
        if script:findFirstChild(plr.Name) then
            for i = 1,#colors do
                if colors[i][1] == script[plr.Name].Value then
                    colors[i][2] = 0
                end
            end
            script[plr.Name]:Destroy()
            if not game.Workspace["Dead_Players"]:findFirstChild(plr.Name) then
                a = Instance.new("StringValue",game.Workspace["Dead_Players"])
                a.Name = plr.Name
                a.Value = plr.Name
            end
        end
    end)

    plr.Chatted:connect(function(msg)
        if script:findFirstChild(plr.Name) then
            if string.lower(msg) == string.lower(game.Workspace.MusicScript.CurrentSong.Value) and game.Workspace.MusicScript.play.Value == true then
                win(plr.Name)
            else
                print("wrong guess")
            end
        else
            print("not in game")
        end
    end)
end)

game.Players.PlayerRemoving:connect(function(plr)
    if script:findFirstChild(plr.Name) then
        for i = 1,#colors do
            if colors[i][1] == script[plr.Name].Value then
                colors[i][2] = 0
            end
        end
        script[plr.Name]:Destroy()
    end
end)

game.Players.PlayerAdded:connect(function(plr)
    y = 0
    while wait(1) do
        y = y + 1
        plr.Chatted:connect(function(msg)
            y = 0
        end)
        if y == 120 then
            plr.Character.Humanoid.Health = 0
            y = 0
        end
    end
end)
0
Can you edit out the script and show us the part of the script your having trouble with? I can't read all of this ;-; laughablehaha 494 — 9y
0
well I don't know what part is causing it cause there are no errors, so NotSoNorm 777 — 9y
0
Can you add prints and find where it fails to work? Lacryma 548 — 9y

1 answer

Log in to vote
0
Answered by 9 years ago

This is pretty difficult since we don't know which part of the code you need fixed but you would use "break" to leave a loop. For example:

for i = 0,10 do
    if i == 6 then
        break
    end
    print(i)
end

This would output:

1 2 3 5

The "break" command exits a loop. So in your case find the part in your code that continues running after you want it to stop, and then set a if condition when you want it to stop and then insert the "break"

Ad

Answer this question