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

Why is this not looping?

Asked by
Kyokamii 133
9 years ago

Im making a for loop and it does not loop like I want it to.

function newRound(plr) --Main function :)
    if serverStarted ~= nil then
        for i=1, 10,1 do
            wait(1)
            updateGui("New round starting in "..timeToWait-1)
        end
    end
end

No outputs

FULL SCRIPT:

--//VALUES\\--
local maps = {"Map1","Map2","Map3","Map4","Map5","Map6"}
mapChosen = ""
roundTime = 1*60 -- 1 minute
timeToWait = 10
serverStarted = false
--//CODE\\--
function newRound(plr) --Main function :)
    if serverStarted ~= nil then
        for i=1, 10, 1 do
            wait(1)
            updateGui("New round starting in "..timeToWait-1)
        end
        mapChosen = maps[math.random(#maps)]
        changeMap(mapChosen)
        updateGui("Map chosen is "..mapChosen)
        wait(3)
        tpPlayers("map")
        updateGui("Round end in 10 seconds") --should be minutes but too long and its late now lmao
        wait(10)
        tpPlayers("lobby")
        updateGui("Round is over!")
        removeMap()
        wait(1)
        newRound(plr)
    end
end
function changeMap(map) --Change the map
    if workspace:FindFirstChild(mapChosen) then
        removeMap()
    else
        local map = game.ReplicatedStorage.Maps:FindFirstChild(mapChosen):Clone()
        map.Parent = workspace
        wait()
    end
end
function updateGui(msg) --Update the message
    for i,v in pairs(game.Players:GetPlayers()) do
        v.PlayerGui:WaitForChild("Gui") --Wait for the GUI
        v.PlayerGui.Gui.Text.Text = msg
        wait()
    end
end
function removeMap() --Remove the map
    if workspace:FindFirstChild(mapChosen) then
        workspace:FindFirstChild(mapChosen):Destroy()
        wait()
    end
end
function tpPlayers(location) --Teleport players to map
    for i,v in pairs(game.Players:GetPlayers()) do
        local chr = v.Character
        if chr then
            if location == "map" then
                chr:MoveTo(Vector3.new(0,0,0))
            elseif location == "lobby" then
                chr:MoveTo(workspace.Lobby.Position)
            end

        else
            print(v.Name.."'s character not found.") --Error message
        end
    end
end
game.Players.PlayerAdded:connect(function(plr)
    serverStarted = true
    newRound(plr)
end)
0
You have to explain more and include all of the relevant code. What's happening? What should be happening insted? BlueTaslem 18071 — 9y
0
This is the only part that does not work. It should be changing the time each second but it changes it once and leaves it there. Kyokamii 133 — 9y

1 answer

Log in to vote
0
Answered by 9 years ago

The problem with your code:

It is continually changing the timeToWait value to 9. So:

local done = false
for i = timeToWait,1,-1 do
    wait(1)
    updateGui(""..i)
    if i == 1 then
        done == true
    end
end
repeat wait() until done == true -- for loops do not stop the code like while loops
0
I won't use your script, but thanks anyway. Kyokamii 133 — 9y
Ad

Answer this question