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

Having a intermission countdown then executing all my code?

Asked by 9 years ago

Hi I was doing a intermission countdown and when it got down to one it stayed at one and then it didn't teleport or have any more hint text that i added. Can you help? Line 17-22

flocal highScore = 10
local Found = nil
local h = Instance.new("Hint",workspace)
local lobbyspawns = workspace.LobbySpawns:GetChildren()
local gamespawns = {workspace.GameSpawn1.Position,workspace.GameSpawn2.Position,workspace.GameSpawn3.Position,workspace.GameSpawn4.Position,workspace.GameSpawn5.Position,workspace.GameSpawn6.Position}
local function getScore(user, stat)
    if user and stat then
        -- if no empty parameters, proceed
        local stats = user:WaitForChild("leaderstats") 
        local score = stats:WaitForChild(stat)

        if score then
            return score.Value
        end
    end
end 
local time2 = 20

for time2 = 20, 1, -1 do
    wait(1)
    h.Text = "Intermission".." ("..time2..")"
end
            print("Intermission")

        game.ReplicatedStorage.Cows:Clone().Parent = workspace
        wait(20)
        h.Text = "Inserting cows..."
        wait(1)
        h.Text = "Milking the cows..."
        wait(1)
        h.Text = "Drinking the milk..."
        wait(1)
        h.Text = "Selling milk..."
        wait(1)
        h.Text = "Making prophet..."
        wait(1)
        h.Text = "Teleporting all players..."
        wait(0.5)
        h.Text = "Starting game..."
local gspawn = gamespawns[math.random(1, #gamespawns)]
                        for i, player in ipairs(game.Players:GetChildren()) do
                                   if player.Character and player.Character:FindFirstChild("Torso") then
                                      player.Character:MoveTo(Vector3.new(3, 1.2, 196)) 
end
        local Time = 300
        while Time > 1 do 
            h.Text = Time .. " seconds left in the game"

                for _, Player in pairs(Game.Players:GetPlayers()) do
                        if Player:FindFirstChild("leaderstats") then
                                if Player.leaderstats["Cow Clicks"].Value >= highScore then -- the error is here, its effecting wait
                                        Found = true
                                        break
                                end
                        end
                end
                if not Found then
                    wait(1)
                    Time = Time - 1
                    print(Time)
                 else
                    break
            end
        end 
        print("Game running")
        for _, player in next, game.Players:GetPlayers() do 
                if getScore(player, "Cow Clicks") >= highScore then 
                print("Game over")
                h.Text = "Game over!"
                local spawn = lobbyspawns[math.random(1, #lobbyspawns)]
                        for i, player in ipairs(game.Players:GetChildren()) do
                                   if player.Character and player.Character:FindFirstChild("Torso") then
                                      player.Character:MoveTo(spawn) 
                                        workspace.Cows:remove()
                                  player.leaderstats["Cow Clicks"].Value = 0
                                end
                        end
                end
        end
end

1 answer

Log in to vote
2
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
9 years ago

Tab your code correctly.

Here's the current shape of your code:

  • intermission
  • post-intermission
  • For each player
    • teleport them
    • start game
    • run game
    • end game

The way it's all in a for loop for the players doesn't make sense.

Here is how your code should be shaped:

  • Teleport each player

  • Run game

    • stop when time runs out, or when a player wins
  • Reset

Here is a paraphrase of the code:

local highScore = 10
local h = Instance.new("Hint", workspace)
local lobbyspawns = workspace.LobbySpawns:GetChildren()
local gamespawns = {
    workspace.GameSpawn1.Position,
    workspace.GameSpawn2.Position,
    workspace.GameSpawn3.Position,
    workspace.GameSpawn4.Position,
    workspace.GameSpawn5.Position,
    workspace.GameSpawn6.Position,
}

local function getScore(user, stat)
    if user and stat then
        local stats = user:WaitForChild("leaderstats")
        local score = stats:WaitForChild(stat)
        if score then
            return score.Value
        end
    end
end

-- Figure out who has won:
-- (returns `nil` if no one has)
function getWinner()
    for _, player in pairs(game.Players:GetPlayers()) do
        if getScore(player, "Cow Clicks") > highScore then
            return player
        end
    end
end


while true do -- do all of this over and over

    -- there is no `time` variable.
    -- `for` loop variables are automatically local already.
    for time = 20, 1, -1 do
        h.Text = time .. " seconds left in the intermission"
        wait(1)
    end

    game.ReplicatedStorage.Cows:Clone().Parent = workspace

    wait(5)
    h.Text = "Starting the game..."
    wait(1)

    -- Teleport all players
    for _, player in pairs(game.Players:GetPlayers()) do
        if player.Character then
            player.Character:MoveTo( Vector3.new(3, 1.2, 196) )
        end
        wait(1) -- EDIT
    end

    -- Run the game
    for time = 300, 1, -1 do
        h.Text = time .. " seconds left"
        if getWinner() then
            break -- ended the game early
        end
    end

    -- Announce winner
    local winner = getWinner()
    if winner then
        h.Text = winner.Name .. " won!"
    else
        h.Text = "Time ran out."
    end

    -- Send everyone home
    for _, player in pairs(game.Players:GetPlayers()) do
        if player.Character then
            local spot = lobbyspawns[math.random(#lobbyspawns)]
            player.Character:MoveTo(spot)
        end
        player.leaderstats["Cow Clicks"].Value = 0
    end


    wait(5)
end

ERRATA:

  • The time = 300, 1, -1 do loop was missing a wait(1). It has been added.
  • Line 27 should be "Cow Clicks" instead of "CowClicks". It has been fixed.
Ad

Answer this question