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

How do I make a timer that works independently of other players?

Asked by 3 years ago

I am making a speedrun game and I want a timer that shows on the leaderboard that keeps track of how long a player has been doing the speedrun and the timer will end once the player has reached the end and touched a block. This is the script I have right now but the issue is that once any player touches the block, the timer ends for everyone. How can I make it individual to the player and will be non-exploitable?

Local Script Inside StarterPlayerScripts

local ReplicatedStorage = game:GetService("ReplicatedStorage")

local addTimer = ReplicatedStorage:WaitForChild("Timer")

local clock = true

-- Fire the remote event
while clock do
    wait(1)
    addTimer:FireServer()
    local endblock = game.Workspace.TeleportBricks.Level1
    endblock.Touched:Connect(function(hit)
        clock = false
    end)
end

Server Script

local ReplicatedStorage = game:GetService("ReplicatedStorage")

local addTimer = ReplicatedStorage:WaitForChild("Timer")

local function AddTime(player)
    local Time = player.leaderstats.RunTime
    print(player.Name .. " has been running for " .. Time.Value .. " seconds")
    Time.Value += 1
end

addTimer.OnServerEvent:Connect(AddTime)

Thanks!

1 answer

Log in to vote
0
Answered by 3 years ago
Edited 3 years ago

put the timer in the player and you don't need a local script for any of that.

0
Could you elaborate on this? I’m pretty new to roblox scripting and I’m not sure what you mean. xboxking2000 2 — 3y
Ad

Answer this question