Code is changing the time of something too slow when it's just right

Hello devs, I am making a script that puts a “Respawning in 5” above the player’s head when dead.

Here is the script:

local respawnTime = game.Players.RespawnTime

local tag = script.Parent.Dead

game.Players.PlayerAdded:Connect(function(p)
	p.CharacterAdded:Connect(function(ch)
		ch.Humanoid.Died:Connect(function()
			local nowTag = tag:Clone()
			local respawnTime2 = respawnTime

			nowTag.Parent = ch.Head
			while wait() do
				if respawnTime2 <= 0 then
					nowTag.LowerText.Text = 0
					break
				else
					wait(0.1)
					respawnTime2 = respawnTime2 - 0.1
					nowTag.LowerText.Text = respawnTime2
				end
			end
		end)
	end)
end)

It goes too slow though but it seems right to me.

Here is the video:

The video wasn’t edited or cut and the respawn time is 5 seconds.

1 Like

It’s because of the while wait() do and your wait(0.1) statement. It should work when you make while true do.

3 Likes

Works but it cuts off at 0.2 seconds. I want to make it exactly perfect

I guess you set the RespawnTime in a ServerScript?

1 Like

A classic example of this:

OP didn’t need a wait() in the loop condition because the body of the loop already yields.

2 Likes

Yeah I did, what changes does it make though?

There is a short delay when the ServerScript changes stuff on your server that the players see. It’s because the players ping isn’t 1.

try using tick() to get the most accurate timing.

local respawnTime = game.Players.RespawnTime
local runService = game:GetService("RunService")
local tag = script.Parent.Dead

game.Players.PlayerAdded:Connect(function(p)
	p.CharacterAdded:Connect(function(ch)
		ch.Humanoid.Died:Connect(function()
			local nowTag = tag:Clone()
			nowTag.Parent = ch.Head
			local respawn = tick() + respawnTime
			local timer
			while (tick() < respawn) do
				timer = respawn - tick()
				timer = math.floor(timer * 10 + 0.5) / 10
				nowTag.LowerText.Text = timer
				runService.Heartbeat:Wait()
			end
			nowTag.LowerText.Text = 0
		end)
	end)
end)
3 Likes

It works perfectly!

Now onto the last thing, when it says 3, I want it to say 3.0, if it’s on 4, 2, 1, 0 or 5 then I want to add .0 to the end of it. How would I do that?

Change these two lines:

timer = math.floor(timer * 10 + 0.5) / 10
nowTag.LowerText.Text = timer

to this:

timer = math.floor(timer + 0.5)
nowTag.LowerText.Text = timer..".0"

I assumed that since you were subtracting 0.1 from the timer in your original code I thought you wanted to include tenths of seconds. Here I changed it so that it only counts seconds and adds the “.0” at the end for aesthetics.

attempt to perform arithmetic (add) on nil and number

local respawnTime = game.Players.RespawnTime
local runService = game:GetService("RunService")
local tag = script.Parent.Dead

game.Players.PlayerAdded:Connect(function(p)
	p.CharacterAdded:Connect(function(ch)
		ch.Humanoid.Died:Connect(function()
			local nowTag = tag:Clone()
			local respawnTime2 = respawnTime

			nowTag.Parent = ch.Head
			local respawn = tick() + respawnTime
			local timer
			while (tick() < respawn) do
				timer = math.floor(timer + 0.5)
				nowTag.LowerText.Text = timer..".0"
				runService.Heartbeat:Wait()
			end
			nowTag.LowerText.Text = "0.0"
		end)
	end)
end)

Errors at line 15 which is timer = math.floor(timer + 0.5)

The body of the while loop should look like this:

			while (tick() < respawn) do
				timer = respawn - tick()
				timer = math.floor(timer + 0.5)
				nowTag.LowerText.Text = timer..".0"
				runService.Heartbeat:Wait()
			end

The error happened because you forgot to copy the line where you set the value for ‘timer’

1 Like

Ok now it doesn’t have the .0 at the end. Oh u edited it hold on

Sorry, edited the post just now.

1 Like

Thanks that works too but I meant to make the .0 bit like a ms bit not just to make it stay at .0

So like “1.9”… “1.8”… “1.7”…

1 Like

Sorry, I didn’t realize what you meant the first time then.

			while (tick() < respawn) do
				timer = respawn - tick()
				timer = math.floor(timer * 10 + 0.5) / 10
				nowTag.LowerText.Text = string.format("%.1f", timer)
				runService.Heartbeat:Wait()
			end

This will count tenths of seconds while always making sure there is a decimal behind the number.

1 Like

Thanks so much @Blokav, your so nice. Have a good day and remember to have fun web dev-ing.