Clock is not working

I am trying to make a clock system though the timer is not working. I am 99% percent sure i am doing nothing wrong. It has never not worked, i am very confused.
The only error i really have is image and i do not think that is related to this.
I even tried opening the PlayerGui under players and that did not worked. I tried local script.
The Text is supposed to change every second though it is staying the same. Code will be down bellow.

local timer = game.StarterGui.ScreenGui.Frame.TextLabel.Text
while true do
	timer = "12:01"
	wait(1)
	timer = "12:02"
end 

I

3 Likes

You have to think of a while true do loop like a loop. For example, your code will do the following

  • timer 12:01
  • wait 1
  • timer 12:02
  • timer 12:01
  • wait 1
  • timer 12:02
  • and so on…

To fix your issue, simply add another wait:

local timer = game.StarterGui.ScreenGui.Frame.TextLabel.Text
while true do
	timer = "12:01"
	wait(1)
	timer = "12:02"
    wait(1) --Added a wait to complete the loop
end 

The error you posted is not related to your code. You can see this in the blue text below, which shows the source of the error.

2 Likes

The text still does not work. I even tried this without being in a loop.
image
I think something wrong on my end.

local timer = game.StarterGui.ScreenGui.Frame.TextLabel.Text
while true do
	timer = "12:01"
	wait(1)
	timer = "12:02"
	wait(1)
end

You’re setting the variable “timer” to whatever is in your text.

I think you need to do this:

local timer = game.StarterGui.ScreenGui.Frame.TextLabel

then refer to timer.text in your loop.

2 Likes

I recommend putting the script inside of the text label and instead of using “game.StarterGui.ScreenGui.Frame.TextLabel.Text” you use script.Parent.Text.

1 Like

I already do have the script inside of the textlabel.

But thank you.

Try replacing game.StarterGui.ScreenGui.Frame.TextLabel.Text with script.Parent.Text. It won’t work otherwise since the gui will be located in the Player’s “PlayerGui” instead of the Starter Gui when a player joins the game.

2 Likes

That does not work either. I can not find something effective.
Also the output is speechless. Useless right now.

If I am correct, make timer refer to script.Parent and then say something like:

timer.Text = "" -- Put whatever you want the text to say in there

Maybe this sounds dumb but maybe it works but you can’t see it because changing the startergui doesn’t change what you see as a player. I think you need to use :WaitForChild(‘PlayerGui’)

I believe your issue may also be that you’re referencing 'StarterGui" in a script. When the game runs this becomes ‘PlayerGui’. As others have said, using script.parent would correct this.

1 Like

You followed the syntax correctly, however you still have many issues here:

Issue 1: Wrong ScreenGui
Anything placed under StarterGui gets cloned to Player.PlayerGui whenever a player joins. game.StarterGui.ScreenGui is not referencing the actual ScreenGui being shown on a given player’s screen, it’s referencing the original source of the ScreenGui. You should type game.Players.LocalPlayer.PlayerGui.ScreenGui instead of game.StarterGui.ScreenGui.

Issue 2A: Setting The Property
I assume your goal is to change the Text property of TextLabel so that it displays different text. When you type timer = , you are not changing the property of the TextLabel instance, you are changing the variable named timer.

Example:

local variable = workspace.Part
variable = workspace.Model

print(variable)

Output:
> Model

Issue 2B: Setting The Property
When you set the variable timer as game.StarterGui.ScreenGui.Frame.TextLabel.Text, you’re setting the variable timer to the property Text of the TextLabel instance. If you want to modify one of an instance’s properties, you have to start with the instance; you can’t start with the property, otherwise you’ll simply be reassigning a variable.

Incorrect example:

local part = workspace.Part.Color
part = Color3.new(1, 1, 1)

print(part)
print(part.Color)

Output:
> 1, 1, 1
> Error: Color is not a valid member of Color3

Correct example:

local part = workspace.Part
part.Color = Color3.new(1, 1, 1)

print(part)
print(part.Color)

Output:
> Part
> 1, 1, 1

If you fixed all of that, the text should change to display “12:01”. Problem is, it stays like that forever.

Issue 3: Looping
Imagine the loop as if you were the machine that was reading and interpreting the code, line-by-line, and try translating it from ROBLOX Lua back into regular English.

Step 1: while true do = “If true is not true, continue to next step, otherwise skip to next end.”
Step 2: timer.Text = "12:01" = “Change the Text of TextLabel to ‘12:01’.”
Step 3: wait(1) = “Wait 1 second.”
Step 4: timer.Text = "12:012 = “Change the Text of TextLabel to ‘12:02’.”
Step 5: end = “Since this is a while loop, go back to step 1 and see if we still need to do it again.”

So, if we were to go through this twice…

Action 1: True is true. Continuing to next step.
Action 2: Changing the text to “12:01”.
Action 3: Waiting 1 second.
Action 4: Changing the text to “12:02”.
Action 5: Going back to step 1.
Action 6: True is still true. Repeating.

Action 7: Changing the text to “12:01”.
Action 8: Waiting 1 second.
Action 9: Changing the text to “12:02”.
Action 10: Going back to step 1.

The first and most obvious observation you likely made was that the text is being set to “12:01”, then being set to “12:02”, then being set back to “12:01” and repeating forever. We don’t want it to be switching back and forth; we want it to be counting up. To do that, you’ll have to add a variable as a number, add to that number each loop, then concatenate that number to a string when setting the text.

local secondsPassed = 0

while true do
	secondsPassed = secondsPassed+1
	timer = "12:"..secondsPassed
	wait(1)
end

The second issue with the loop (as @ThatTimothy explained) was the lack of a second wait, but that isn’t applicable here anymore since we’ve changed the code.

Despite all of this writing, I still haven’t explained every issue that will come up here. After 9 seconds, your timer will start displaying “12:010”, and after 60 seconds it’ll display “12:060”. It would be too time-consuming for me to guide you through this entire thing in one reply, so you’ll have to figure out some of it on your own.

If you’re still confused about any of this, check out these sources and see if they’re any help:

Variables Variables
Loops Loops
Strings Strings

1 Like

Nevermind i found a solution to my issue. After doing a little research i replaced my code with a local script and a script inside workspace containing scripts.

local script

local status = game.ReplicatedStorage:WaitForChild("Value")

script.Parent.Text = status.Value
status.Changed:Connect(function()
	script.Parent.Text = status.Value
end)

script

local status = game.ReplicatedStorage:WaitForChild("Value")
	while true do 
	status.Value = "12:01"
	wait(1)
	status.Value = "12:02"
	wait(1)
end

Messing with StarterGui after the game has already run is like modifying the starter kit to a pvp game when the game has already finished. When editing the StarterGui you are basically editing what new players would see on screen as oppose to the client’s/player’s screen. You need to use
“game.Players.LocalPlayer.PlayerGui.ScreenGui.Frame.TextLabel.Text” for it to change anything on the the player’s screen. Hope I helped!

1 Like