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

How do I make sure this countdown doesn't go faster with more players?

Asked by 4 years ago

I'm doing something that involves a countdown when a player enters a truck. I know why this is happening, it's because it's inside the loop and gets stacked every time a new player enters. I just don't know how to fix it. Maybe you guys can help me out?

local p = script.Parent
local t = workspace.Truck2
local s = t.Seats:GetChildren()
local bg = workspace.BG1.BillboardGui
local te = game.ReplicatedStorage:FindFirstChild("TeleportEvent")
local ge = game.ReplicatedStorage:FindFirstChild("GuiEvent")
local pe = game.ReplicatedStorage:FindFirstChild("PalmEvent")
local ple = game.ReplicatedStorage:FindFirstChild("PlayerLeaveEvent")

local numplayers = 0

local timetodepart = 20

p.Touched:Connect(function(hit)
    local player = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent)
    ge:FireClient(player)
end)


te.OnServerEvent:Connect(function(teleplayer)
    if teleplayer.Character:FindFirstChild("Humanoid") then
        local realplayer = game.Players:GetPlayerFromCharacter(teleplayer)
        numplayers = numplayers + 1
        for i,v in pairs(s) do
            if s.Occupant == nil then
                teleplayer.Character.Humanoid.WalkSpeed = 0
                teleplayer.Character.Humanoid.JumpPower = 0
                bg.TextLabel.Text = "Join the Ride!        Players: ".. numplayers .."/19"
                teleplayer.Character.HumanoidRootPart.CFrame = s[math.random(1,19)].CFrame
                break
            end
        end
        for i = 1,21 do --HERE IS THE COUNTDOWN
            bg.TextLabel.Text = "Starting in ".. timetodepart .." seconds with: ".. numplayers .." players!"
            timetodepart = timetodepart - 1
            wait(1)
        end
        bg.TextLabel.Text = "Starting with: ".. numplayers .."/19 players!"
        for i = 1,100 do
            t:TranslateBy(Vector3.new(-2,0,0))
            wait(0.05)
        end
    end
end)

Script involves clicking a GUI to be teleported into the truck, which fires a RemoteEvent from the client to the server to tell it that the player has clicked the "Join" button. I would place it outside, but I don't want to start the countdown without a player clicking.

1
You need to detect if the code is already running before starting it again. Sort of like a denounce OBenjOne 190 — 4y
0
Thanks my man! MustangHeart 67 — 4y

Answer this question