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)
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
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
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)
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.
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?