When stopwatch script is stopped, it stops for all players. How to fix this?

Hello! I made a stopwatch for my racing game. I’ve encountered a problem, if one player touches the stop part (pauses stopwatch) it pauses for every player. How can I fix this?

The script I made is this:

-- References to Events (Removed these references)
local frame = script.Parent.Stopwatch
local timeLabel = frame.TimeLabel

-- Variables for time tracking
local isRunning = false
local startTime = 0
local elapsedTime = 0
local timer

-- Function to update the time display
local function updateTimeDisplay(timeLabel)
	local totalMilliseconds = math.floor(elapsedTime * 1000)
	local hours = math.floor(totalMilliseconds / 3600000)
	local minutes = math.floor((totalMilliseconds % 3600000) / 60000)
	local seconds = math.floor((totalMilliseconds % 60000) / 1000)
	local milliseconds = totalMilliseconds % 1000
	timeLabel.Text = string.format("%02d:%02d:%02d.%03d", hours, minutes, seconds, milliseconds)
end

-- Get the LocalPlayer
local player = game.Players.LocalPlayer

-- Get the character of the LocalPlayer
local character = player.Character

-- Store original WalkSpeed and JumpPower
local originalWalkSpeed = nil
local originalJumpPower = nil

-- Function to freeze the character
local function freezeCharacter(character)
	-- Check if the character exists
	if character then
		-- Get the Humanoid of the character
		local humanoid = character:FindFirstChildOfClass("Humanoid")

		-- Check if the Humanoid exists
		if humanoid then
			-- Store the original WalkSpeed and JumpPower if not already stored
			if not originalWalkSpeed then
				originalWalkSpeed = humanoid.WalkSpeed
				originalJumpPower = humanoid.JumpPower
			end

			-- Set the Humanoid's WalkSpeed and JumpPower to 0 to freeze movement
			humanoid.WalkSpeed = 0
			humanoid.JumpPower = 0
		end
	end
end

-- Function to unfreeze the character
local function unfreezeCharacter(character)
	-- Check if the character exists
	if character then
		-- Get the Humanoid of the character
		local humanoid = character:FindFirstChildOfClass("Humanoid")

		-- Check if the Humanoid exists and original values are stored
		if humanoid and originalWalkSpeed and originalJumpPower then
			-- Restore the original WalkSpeed and JumpPower
			humanoid.WalkSpeed = originalWalkSpeed
			humanoid.JumpPower = originalJumpPower

			-- Reset original values
			originalWalkSpeed = nil
			originalJumpPower = nil
		end
	end
end


-- Function to start the timer
local function startTimer()
	if not isRunning then
		isRunning = true

		freezeCharacter(character)

		task.wait(2)

		unfreezeCharacter(character)

		startTime = tick() - elapsedTime
		timer = game:GetService("RunService").RenderStepped:Connect(function()
			elapsedTime = tick() - startTime
			updateTimeDisplay(timeLabel) -- Pass the timeLabel to updateTimeDisplay
		end)
	end
end

-- Function to stop the timer
local function stopTimer()
	if isRunning then
		isRunning = false
		timer:Disconnect()
	end
end

-- Function to reset the timer
local function resetTimer()
	stopTimer()
	elapsedTime = 0
	updateTimeDisplay(timeLabel) -- Pass the timeLabel to updateTimeDisplay
end


-- Connect the start event directly to the touch event
local startpart = game.Workspace.Start 
startpart.Touched:Connect(startTimer)


-- Connect the stop event directly to the touch event
local stopPart = game.Workspace.Stop
stopPart.Touched:Connect(stopTimer)


-- Connect the stop event directly to the touch event
local resetpart = game.Workspace.Reset
resetpart.Touched:Connect(resetTimer)

-- Initialize display (Remove this line if "TimeLabel" is no longer used)
updateTimeDisplay(timeLabel) -- Pass the timeLabel to initialize display

It is a localscript that controls 3 Bindable events. One that starts the stopwatch (startTimer), One that resets the timer (resetTimer), and one that stops the timer (stopTimer). I’m trying to get the stopTimer event to be separate for every player.

Appreciate the input!

is this all a local script?
strong text

Check if the player that touched the part and local player is the same players

1 Like