Stopwatch script problems

I’m creating a stopwatch script, and it isn’t working quite right.
Here is the layout:
ScreenGui
NumberValue (Named as Time)
TextLabel
LocalScript

Now, whatever I make NumberValue is what TextLabel starts as, and I want that to be 0.
But I have noticed it only will count up to double the original number, which, for 0, is 0.
Script:
local seconds = script.Parent.Parent.Time

script.Parent.Text = seconds.Value

for i = 1,seconds.Value do

wait(1)

seconds.Value = seconds.Value + 1

script.Parent.Text = seconds.Value

end

1 Like

Please format the code right so we can read it easier :smile:

Er…not trying to be rude, but how do I format the code? I haven’t been asked this before.

Don’t worry! You’re not being rude :smile:.

On the topbar, there should be a symbol that looks like this “</>”, highlight the text that you want formatted and click that button (sorry if this wasn’t very clear)

Based on your layout, wouldn’t script.Parent.Parent lead to NumberValue (Time) ? So, why the script.Parent.Parent.Time?

Did you mean to do script.Parent.Parent.Parent.Time?

local seconds = script.Parent.Parent.Parent.Time

script.Parent.Text = seconds.Value

for i = 1,seconds.Value do
wait(1)
seconds.Value = seconds.Value + 1
script.Parent.Text = seconds.Value
end
Alright, there it is!

I’m not the best scripter, so I actually just modified a tutorial for a timer, and it didn’t work.

I suggest using while wait(1) do, using for will only loop codes n times, which in this case in 0, so it doesn’t run.

1 Like
local seconds = script.Parent.Parent.Time

script.Parent.Text = seconds.Value

while wait(1) do
    seconds.Value = seconds.Value + 1   
    script.Parent.Text = seconds.Value

    if --the stopwatch stop button is clicked
                                              then --put this then in the line above, i put it here to not confuse you with the comment
        break
    end
end

This is what i would try doing, I’m not sure if it would work.

1 Like
--[ Variables
local val =  --path your numbervalue
local text =  --path your textlabel
local stop =  --path your stop button
local paused = false
--[ Setup
stop.Activated:Connect(function()
	if not paused then
		paused = true
	else
		paused = false
	end
end)
while wait(1) do
	if not paused then
		val.Value += 1
		text.Text = val.Value
	end
end

Thanks a lot! Worked perfectly!

1 Like

ayyyy :grin: No problem! Always happy to help.