I need help fixing my stopwatch

Hello former developers! I ran into an error on my stopwatch that I am making for my next game. It does go when the server event is fired, it just doesn’t add right. Here is a little video.


(Sorry for the bad quality, I used the Video Record feature in Studio. )

Here is the code used:

local rtime = script.Parent.Time

game.ReplicatedStorage.PlayersGo.OnServerEvent:Connect(function()
	while true do
		rtime.Text = rtime.Text + 0.001
		wait(0.001)
	end
end)

If you can help, please let me know. Thanks!
-WE

3 Likes

wait() can’t go below 0.03 seconds.

Do you have a solution for the problem?

os.time or tick() could help, although I haven’t used them before so i can’t help.

local I = 0
local MaxTime = 5
while true do
	wait(0.001)
	I += 0.001
	local dec = I - math.floor(I)
	if dec > 0.100 then
		I = 1 + math.floor(I)
	end
	rtime.Text = tostring(I)
	if I >= MaxTime then
		break
	end
end

os.time is for live events, but I do not know what tick() is completly for yet.

as I said, I haven’t used them also I said could so you didn’t need to really correct me.

Yeah that’s one use for it although that doesn’t eliminate its use here.

Read here for what tick is and you’ll find it can have a similar use case to os.time()

Tick would probably be the easier one for this use case but it’s up to you. If it’s just for a delta of time then either one would work, but tick is local time and os.time is usually UTC.

local I = 0
local MaxTime = 5
while true do
	wait(0.001)
	I += 0.001
	local dec = I - math.floor(I)
	if dec > 0.100 then
		I = 1 + math.floor(I)
	end
	local buf = ""
	if 4 - string.len(tostring(I)) == 3 then
		buf = buf .. ".000"
	end
	if 4 - string.len(tostring(I)) == 2 then
		buf = buf .. "00"
	end
	if 4 - string.len(tostring(I)) == 1 then
		buf = buf .. "0"
	end
	script.Parent.Text = tostring(I)..buf
	if I >= MaxTime then
		break
	end
end

Timer.rbxl (21.7 KB)

Just make the timer show how much time was elapsed:

local rtime = script.Parent.Time

game.ReplicatedStorage.PlayersGo.OnServerEvent:Connect(function()
    local StartTime = tick()
	while true do
		rtime.Text = math.floor((tick() - StartTime) * 10)/10
		wait()
	end
end)

tick() is good for things like these.

1 Like

Umm so yeah…
image

@SSSpencer413 told me that tick() is another functuon of os.time

So 159933745.6 is the unix time right now.

1 Like

RunService.Heartbeat is probably what you want since it returns the time that has passed between when it was last fired.

local CurrentTime = 0
while true do
    local DeltaTime = game:GetService("RunService").Heartbeat:Wait()
    CurrentTime = CurrentTime + DeltaTime
end
1 Like

Hey, WEcompany!

I have come up with a solution to accommodate your issue, plus some suggestions on the side to help you with your game development journey.

Here is the code:

--//Variables\\--
local gui = script.Parent
	local label = gui:WaitForChild("Label", 30)
	local swValue = gui:WaitForChild("StopwatchValue", 30) --// We'll use this NumberValue object to display the time.

local timeFormat = "%.2f" --// A string formatting pattern to easily convert the values to display specific decimal places.

--//Services
local TweenService = game:GetService("TweenService") --// The backbone of the operation, essentially what makes the stopwatch display accurate results.

--//Shorthands (makes the code run slightly faster)
local tostr = tostring
local format = string.format

--//Tween Info
local stopwatchInfo = TweenInfo.new(
	6000, --// Set this value to be as large or small as you like.
	Enum.EasingStyle.Linear,
	Enum.EasingDirection.InOut
)

local stopwatchTween = TweenService:Create(swValue, stopwatchInfo, {Value = 6000}) --// Same here. (Maybe declare a variable for this.)

--//Custom Functions\\--
function ValueChanged(value)
	label.Text = format(timeFormat, tostr(value))
	--// Display the time from the value object and format it.
end

function InitiateStopwatch()
	stopwatchTween:Play()
	swValue.Changed:Connect(ValueChanged)
	--// Every time the value object's value changes, call the ValueChanged function.
end

--//Connections\\--
wait(3)
InitiateStopwatch()


To sum up the above, we are using the TweenService to actually accumulate the swValue object’s value. We do not have to rely on the wait function as that can heavily vary depending on the game’s performance status.

The result: an accurate display of how much time as passed!

You may of course modify the code above to fit into your game as needed. I would also like to give you a couple suggestions:

  • Do not fire RemoteEvent objects multiple times in rapid succession. This will cause performance issues within your game.
  • Try to track the stopwatch’s activity on the server-side. It will save some work for you, plus you can accurately show each player’s total time when they complete the race course. Just grab the time at the exact moment a player does said action.

I hope you found my assistance useful on your development journey. If you need additional help, feel free to reach out to me.

Keep up the good work!
-theplushedguy3 :star2:

P.S. - That race course looks quite nice. :wink:

3 Likes

The thing is that the counter starts when the server RemoteEvent is fired, and I want it to go up by 0.01 every 0.01 seconds. When I configure everything right, it goes up by 1 for every 1 second. Do you have a fix to this?

1 Like

Can you confirm you have tested my sample code? When I tested it, everything seemed to be working fine.

Also, accessing a time interval of 0.01 seconds isn’t possible with using the wait function.

2 Likes

1 Like

Is the value object’s class a NumberValue or an IntValue? The code is only compatible with a NumberValue object.

2 Likes

It was a intvalue. Changing it to NumberValue.

1 Like

I see you are satisfied. I’m glad I was able to help you!

1 Like