Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

What Is Wrong With My Timer? (Also, 1 More Question)

So... I Made A Countdown Timer From A YT Tutorial And It Stops At Exactly 54 Seconds... Idk Why But Here it is...

local seconds = script.Parent.Parent.Time

script.Parent.Text = seconds.Value

for i = 0.1,seconds.Value do
 wait(0.1)
 seconds.Value = seconds.Value - 0.1
 script.Parent.Text = seconds.Value
end

Can Someone PLS Fix It Because I Got No Coding Experience.. And Also, I Want To Make It When The Time Finishes, The Player Dies... Someone Pls Help :(

13 comments

0
  • I edited my answer.
0

What is Time? Is it a number value or what?

0

Yes, A Number Value

0

it stops at 54 seconds because as you are adding 1 to 0.1, the 0.1 becomes the 6 second difference after looping,

0

you can fix it by doing for i = 0.1,seconds.Value,0.1 do

0

It might help if you actually tell us what seconds.Value initial is, as well as your goal.

0

Here Is The YT Video Link. Just Modified The Timer So Its More Detailed (25.3 Instead Of 25) https://www.youtube.com/watch?time_continue=72&v=flRfJBP3kdw&feature=emb_logo

0

I’ve resolved your problem

0

I did it first ;)

0

Sure you may have, but you don’t explain the issue, nor are you using efficient practices.

0

Do you desire you have a number such as 59.9?

0

yes because the whole game is timed and it should be detailed because it looks cooler. (Im Making A Action Game Where You Have To Get To The End Before The Time Runs Out And Its My First Game Ever)

0

also, i forgot to mention.. its all about beating high scores so yeah its an important part

4 answers

✓ Accepted answer

Try This! (Reposting with optimized and edited code) Uncopylocked Place here: https://www.roblox.com/games/4983298584/Timer

local seconds = 20

for i = 1,seconds*10,1 do
	wait(0.1)
	seconds = seconds - 1/10
	script.Parent.Text = math.floor(seconds*10)/10
	if seconds <= 0.1 then
		script.Parent.Text = "Times Up!"
		local Head = game.Players.LocalPlayer.Character:FindFirstChild("Head")
		if Head then
			Head:Destroy()
		end
    	end
end

6 comments

0

Still dysfunctional.

0

no it isnt

0

whats wrong about it?

0

you are having a much greater reputation so you may be correct but i dont see anything wrong here

0

print(math.floor(seconds*10)/10)

0

that is actually good, it rounds the seconds to first decimal place so the floats do not appear like this: 8.7999999999990

The problem behind your issue, is that your loop doesn’t compensate for the decimal decrementation. The overall iterations of #n will remove a total of n*.1 from n initial: 60-6 = 54. You need to format the loop to subtract from #n by .1 to result in the total number of proper iterations to reach 0.0:

local Seconds = 60

for Second = 60,0,-.1 do
    Seconds = Second
    print(Second)
wait(.1) end

The issue that is occurring with everyone else’s examples, it that they’re subtracting by i. This parameter is not static, meaning you’ll be subtracting by a number that isn’t in a reliable state; the more i increases, the larger the number subtracted from #n


local Player = game:GetService("Players").LocalPlayer
local PlayerGui = Player:WaitForChild("PlayerGui")
local TimeGui = PlayerGui:WaitForChild("TimeGui")
local Seconds = TimeGui:WaitForChild("Seconds")
local TimeText = script.Parent

local Character = Player.Character or Player.CharacterAdded:Wait()

for Second = Seconds.Value, 0, -.1 do
    Seconds.Value = math.floor(Second*10)/10
    TimeText.Text = math.floor(Second*10)/10
wait(.1) end

Character.Humanoid:TakeDamage(100)

A final note: Decreasing by .1 by .1ms will = 1 second, meaning you’re not actually speeding up the counter.


If you don’t have a proper understanding on loops in Lua, I suggest taking a gander at the hyperlink provided, or this simple answer on someones question. Standard for loop syntax goes by Start Stop Step (Optional).

If you try to compare that setup with some answers below, you may understand why they didn’t function properly.

2 comments

0

You Explained Everything But Either I Didn't Use It Properly Or It Just Doesn't Work. The Time Is Always 0 With This Code And When I Copied Both Of Ur Codes, The Time Was Showed In Output And At 20 It Got .99999999 Numbers. Sorry But Oilkas Wins The ''Accepted Answer''. Tell Me If I Did Something Wrong.

0

Apologies, I forgot to cap to a single float.

local Timer = 20

for i = 0, Timer, 0.1 do
    wait(.1)
    Timer = Timer - 0.1
    
    if math.floor(Timer) == 0 then
        game.Players.LocalPlayer.Character.Head:Destroy()
    end
end

2 comments

0

Doesn't Work. It Makes The Time Always 0

0

Still Makes The Time 0

local seconds = script.Parent.Parent.Time

script.Parent.Text = seconds.Value

for i = seconds.Value, 0, -0.1 do --//Time will go down by 0.1
	wait(0.1) --//Time will go down by 0.1 every 0.1 seconds
	seconds.Value = i
	script.Parent.Text = seconds.Value
end

Try this and feel free to comment on my answer if there is a problem. Maybe I could fix it.

5 comments

0

The Time Is Always 60

0

If It Helps, The Time Is NumberValue

0

also, the video from what i made it. https://www.youtube.com/watch?time_continue=72&v=flRfJBP3kdw&feature=emb_logo (i changed the numbers so they are more detailed 25.5 instead of 25)

0

should have been seconds.Value,0,-0.1

0

Oh yeah

This discussion is preserved as it was. New answers, comments, and votes are closed.