Small problem with the timer resetting for everyone whenever someone walks into the start part!

I have a little problem with the timer resetting for everyone whenever someone walks through the “starttimer” block. The timer works flawlessly though but that’s the only problem I ran into! I tried tampering with the “onStartTouched” function and local lines at the top, but I ruined the whole thing when I tested it, so I changed everything back to what it is below! :sunglasses:

local startTime = 0
local timerStopped = true
local Runservice = game:GetService("RunService")
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local personalBest = player:WaitForChild("PersonalBest")
local replicatedStorage = game:GetService("ReplicatedStorage")

local function secondsToMinSecMs(seconds)
	local minutes = math.floor(seconds / 60)
	local remainingSeconds = seconds % 60
	local secondsSplit = math.floor(remainingSeconds)
	local millisecondsSplit = math.floor((remainingSeconds - secondsSplit) * 100)

	return string.format("%02i:%02i.%02i", minutes, secondsSplit, millisecondsSplit)
end

local textLabel = script.Parent
textLabel.Text = "Timer: " .. secondsToMinSecMs(currentTime) .. "\nPersonal Best: " .. secondsToMinSecMs(personalBest.Value)

personalBest.Changed:Connect(function()
	personalBest.Value = math.clamp(personalBest.Value, 1/math.huge, math.huge)
	-- if number is less than the smallest positive number possible (x <= 0)
	-- then the line above will make number the smallest positive number possible (x ≈ 0, x > 0)
	-- peventing personal best to be negative numbers or exactly 0
	
	textLabel.Text = "Timer: " .. secondsToMinSecMs(currentTime) .. "\nPersonal Best: " .. secondsToMinSecMs(personalBest.Value)
end)

local function onStartTouched()
	currentTime = 0
	startTime = tick()
	timerStopped = false
end

local function onEndTouched()
	timerStopped = true
	if currentTime < personalBest.Value then
		personalBest.Value = currentTime
		replicatedStorage.BestTime:FireServer(currentTime)
	end
	textLabel.Text = "Timer: " .. secondsToMinSecMs(currentTime) .. "\nPersonal Best: " .. secondsToMinSecMs(personalBest.Value)
end

Runservice.RenderStepped:Connect(function()
	if not timerStopped then
		currentTime = tick() - startTime
		textLabel.Text = "Timer: " .. secondsToMinSecMs(currentTime) .. "\nPersonal Best: " .. secondsToMinSecMs(personalBest.Value)
	end
end)

workspace:WaitForChild("StartTimer").Touched:Connect(onStartTouched)
workspace:WaitForChild("EndTimer").Touched:Connect(onEndTouched)

You need to make sure that the part touching is not another player. You can do this by adding the parameter hit (or whatever you want to name it) into these functions and checking if its parent is the local player’s character. If not, then it should not run. Here’s an example:

local function onStartTouched(hit)
	if hit.Parent ~= player.Character then
		return
	end
	print("This code runs when the local player's character touches the part")
end

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.