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

Problem with my round code?

Asked by
R_alatch 394 Moderation Voter
8 years ago

Whenever it gets time to do roundtime, it already says the round is over. Help?

players = game.Players:GetPlayers()
h = Instance.new("Hint", workspace)

maps = game.ServerStorage:FindFirstChild("Maps"):GetChildren()
getsword = game.ServerStorage:FindFirstChild("Sword")

while wait(1) do
    if game.Players.NumPlayers >= 2 then
        h.Text = "Starting a new round!"
        wait(3)

        h.Text = "Choosing a random map..."
        wait(3)

        local chosenmap = maps[math.random(1, #maps)]:Clone()
        chosenmap.Parent = workspace

        h.Text = "The map is " .. chosenmap.Name .. " by " .. chosenmap.Creator.Value .. "!"
        wait(3)

        h.Text = "Teleporting players..."
        wait(3)

        for i, v in pairs(players) do
            if v.Character.Humanoid.Health > 0 and v ~= nil then
                local spawns = chosenmap:FindFirstChild("Spawns"):GetChildren()
                local sword = getsword:Clone()
                v.Character:MoveTo(spawns[i].Position)
                sword.Parent = v.Backpack
            end
        end

        local roundtime = 60
        local dead = 0

        while wait(1) and roundtime > 0 and dead < #players do
            h.Text = "Time until next round " .. roundtime
            roundtime = roundtime - 1
        end

        for i, v in pairs(players) do
            v.Character.Died:connect(function()
                dead = dead + 1
            end)
        end

        h.Text = "The time is up!"
        wait(3)

        for i, v in pairs(players) do
            if v.Character.Humanoid.Health > 0 and v ~= nil then
                v:LoadCharacter()
            end
        end
    else
        h.Text = "Waiting for another player..."
    end
end

1 answer

Log in to vote
1
Answered by 8 years ago

You're doing GetPlayers() right when the server starts, which will always be 0. Let me fix this up for you. I actually also found a few other errors I fixed that had nothing to do with what you were wanting.

h = Instance.new("Hint", workspace)

maps = game.ServerStorage:FindFirstChild("Maps"):GetChildren()
getsword = game.ServerStorage:FindFirstChild("Sword")

while wait(1) do
    if game.Players.NumPlayers >= 2 then
        h.Text = "Starting a new round!"
        wait(3)

        h.Text = "Choosing a random map..."
        wait(3)

        local chosenmap = maps[math.random(1, #maps)]:Clone()
        chosenmap.Parent = workspace

        h.Text = "The map is " .. chosenmap.Name .. " by " .. chosenmap.Creator.Value .. "!"
        wait(3)

        h.Text = "Teleporting players..."
        wait(3)
    local players=game.Players:GetPlayers() --set the players value here
        for i, v in pairs(players) do
            if v.Character.Humanoid.Health > 0 and v ~= nil then
                local spawns = chosenmap:FindFirstChild("Spawns"):GetChildren()
                local sword = getsword:Clone()
                v.Character:MoveTo(spawns[i].Position)
                sword.Parent = v.Backpack
            end
        end

        local roundtime = 60
        local dead = 0

        while wait(1) and roundtime > 0 and dead < #players do
            h.Text = "Time until next round " .. roundtime
            roundtime = roundtime - 1
        end

        for i, v in pairs(players) do
    if v and v.Character and v.Character:FindFirstChild("Humanoid") then --added failcheck
            v.Character.Humanoid.Died:connect(function() --died is an event in humanoid, not player
                dead = dead + 1
            end)
    end
        end

        h.Text = "The time is up!"
        wait(3)

        for i, v in pairs(players) do
            if v.Character.Humanoid.Health > 0 and v ~= nil then
                v:LoadCharacter()
            end
        end
    else
        h.Text = "Waiting for another player..."
    end
end
0
can you upvote and accept the answer please? TheGuyWithAShortName 673 — 8y
Ad

Answer this question