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

How would I make a stopwatch? My current script isn't working?

I want to make a stopwatch and my script looks fine but it isn't.

local frame = script.Parent
local ifPause = frame.pauseAppear
local StartButton = frame.Start
local ResetButton = frame.Reset
local timeText = frame:WaitForChild("Time")
local secondsVal = frame.Seconds

local seconds = 0
local minutes = 0
local hours = 0

timeText.Text = "0:00:00"

local UIS = game:GetService("UserInputService")

UIS.InputBegan:Connect(function(input, gameProcessed)
	if input.KeyCode == Enum.KeyCode.Q then
		ifPause.Value = not ifPause.Value
		if ifPause.Value == true then
			while wait(1)do
				print("seconds going up")
				secondsVal.Value = secondsVal.Value + 1
				if secondsVal.Value == 60 then
					minutes = minutes + 1
					secondsVal.Value = 0
				end
				if secondsVal.Value < 10 then
					if minutes < 10 then	
						timeText.Text = hours..":0"..minutes..":0"..secondsVal.Value
					else
						timeText.Text = hours..":"..minutes..":0"..secondsVal.Value
					end
				else
					if minutes < 10 then
						timeText.Text = hours..":0"..minutes..":"..secondsVal.Value
					else
						timeText.Text = hours..":"..minutes..":"..secondsVal.Value
					end
				end
				ifPause.Value = true
			end
		else
			print("VALUE IS FALSE!!")
			local currentVal = secondsVal.Value
			print(currentVal.." VS "..secondsVal.Value --//Prints
			while secondsVal.Value ~= currentVal do
				print(currentVal.." VS2 "..secondsVal.Value) --//Does not print and I don't know why.
			end
		end
	end
end)

Also I think this way of doing it is not a good way. It would help if you gave me ideas to do it a better way?

Also this is my explorer

Thanks alot for taking your time in reading my message

1 answer

You should be getting an error. I noticed that when I put your code into Studio, there was a syntax error highlighted.

If you are not getting an error, then I don't know why. The error occurs on line 45. You are missing a closing bracket:

Original:

local frame = script.Parent
local ifPause = frame.pauseAppear
local StartButton = frame.Start
local ResetButton = frame.Reset
local timeText = frame:WaitForChild("Time")
local secondsVal = frame.Seconds

local seconds = 0
local minutes = 0
local hours = 0

timeText.Text = "0:00:00"

local UIS = game:GetService("UserInputService")

UIS.InputBegan:Connect(function(input, gameProcessed)
    if input.KeyCode == Enum.KeyCode.Q then
        ifPause.Value = not ifPause.Value
        if ifPause.Value == true then
            while wait(1)do
                print("seconds going up")
                secondsVal.Value = secondsVal.Value + 1
                if secondsVal.Value == 60 then
                    minutes = minutes + 1
                    secondsVal.Value = 0
                end
                if secondsVal.Value < 10 then
                    if minutes < 10 then    
                        timeText.Text = hours..":0"..minutes..":0"..secondsVal.Value
                    else
                        timeText.Text = hours..":"..minutes..":0"..secondsVal.Value
                    end
                else
                    if minutes < 10 then
                        timeText.Text = hours..":0"..minutes..":"..secondsVal.Value
                    else
                        timeText.Text = hours..":"..minutes..":"..secondsVal.Value
                    end
                end
                ifPause.Value = true
            end
        else
            print("VALUE IS FALSE!!")
            local currentVal = secondsVal.Value
            print(currentVal.." VS "..secondsVal.Value --//Prints
            while secondsVal.Value ~= currentVal do
                print(currentVal.." VS2 "..secondsVal.Value) --//Does not print and I don't know why.
            end
        end
    end
end)

Fixed:

local frame = script.Parent
local ifPause = frame.pauseAppear
local StartButton = frame.Start
local ResetButton = frame.Reset
local timeText = frame:WaitForChild("Time")
local secondsVal = frame.Seconds

local seconds = 0
local minutes = 0
local hours = 0

timeText.Text = "0:00:00"

local UIS = game:GetService("UserInputService")

UIS.InputBegan:Connect(function(input, gameProcessed)
    if input.KeyCode == Enum.KeyCode.Q then
        ifPause.Value = not ifPause.Value
        if ifPause.Value == true then
            while wait(1)do
                print("seconds going up")
                secondsVal.Value = secondsVal.Value + 1
                if secondsVal.Value == 60 then
                    minutes = minutes + 1
                    secondsVal.Value = 0
                end
                if secondsVal.Value < 10 then
                    if minutes < 10 then    
                        timeText.Text = hours..":0"..minutes..":0"..secondsVal.Value
                    else
                        timeText.Text = hours..":"..minutes..":0"..secondsVal.Value
                    end
                else
                    if minutes < 10 then
                        timeText.Text = hours..":0"..minutes..":"..secondsVal.Value
                    else
                        
                    end
                end
                ifPause.Value = true
            end
        else
            print("VALUE IS FALSE!!")
            local currentVal = secondsVal.Value
            print(currentVal.." VS "..secondsVal.Value) --//Prints
            while secondsVal.Value ~= currentVal do
                print(currentVal.." VS2 "..secondsVal.Value) --//Does not print and I don't know why.
            end
        end
    end
end)

The fixed version works perfectly fine for me. Although, if you still have issues, feel free to clarify your problem.

If this helps you, feel free to accept the answer.

2 comments

0

The line 47 on your fixed one didn't work

1

As I already commented on the fixed version, it doesn't print to the console, however, it works perfectly fine displaying to a GUI.

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