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

Why does this GUI not update properly?

I'm trying to make a clock that reflects in-game time. It should work fine from what I've seen, except it doesn't. Here's the code:

hour = game.Workspace.Time.Hour.Value

while true do
	wait(1)
	if hour == 0 or 12 then
		script.Parent.Text = "12:00"
	elseif hour == 1 or 13 then
		script.Parent.Text = "01:00"
	elseif hour == 2 or 14 then
		script.Parent.Text = "02:00"
	elseif hour == 3 or 15 then
		script.Parent.Text = "03:00"
	elseif hour == 4 or 16 then
		script.Parent.Text = "04:00"
	elseif hour == 5 or 17 then
		script.Parent.Text = "05:00"
	elseif hour == 6 or 18 then
		script.Parent.Text = "06:00"
	elseif hour == 7 or 19 then
		script.Parent.Text = "07:00"
	elseif hour == 8 or 20 then
		script.Parent.Text = "08:00"
	elseif hour == 9 or 21 then
		script.Parent.Text = "09:00"
	elseif hour == 10 or 22 then
		script.Parent.Text = "10:00"
	elseif hour == 11 or 23 then
		script.Parent.Text = "11:00"
	end
end

I put some prints in there for debugging and the script runs fine, except for one thing... If I update the text manually, it will set it back to 12:00 no matter what. No matter what the hour value is at, it's going to set it to 12. I'm really frustrated and confused because there doesn't seem to be any problem with the script.

2 comments

1

You have to update the value of hour in the loop. Otherwise, it'll set it once and always use that value. Put it right after the wait. Also, you can't say `hour == 1 or 13, you must do hour == 1 or hour == 13.

0

Solved my problem, thanks

1 answer

A little note from experience, you can't really assign variables to properties. It's just not going to work properly. EDIT: I lied a little, you have to update the value every loop.

local hour = game.Workspace.Time.Hour

while wait(1) do
    if hour.Value == 0 or 12 then
        script.Parent.Text = "12:00"
    elseif hour.Value == 1 or 13 then
        script.Parent.Text = "01:00"
    elseif hour.Value == 2 or 14 then
        script.Parent.Text = "02:00"
    elseif hour.Value == 3 or 15 then
        script.Parent.Text = "03:00"
    elseif hour.Value == 4 or 16 then
        script.Parent.Text = "04:00"
    elseif hour.Value == 5 or 17 then
        script.Parent.Text = "05:00"
    elseif hour.Value == 6 or 18 then
        script.Parent.Text = "06:00"
    elseif hour.Value == 7 or 19 then
        script.Parent.Text = "07:00"
    elseif hour.Value == 8 or 20 then
        script.Parent.Text = "08:00"
    elseif hour.Value == 9 or 21 then
        script.Parent.Text = "09:00"
    elseif hour.Value == 10 or 22 then
        script.Parent.Text = "10:00"
    elseif hour.Value == 11 or 23 then
        script.Parent.Text = "11:00"
    end
end
-- You MUST only assign Variables through Objects when it comes to what you're doing

Edit: You could also try .Changed

local hour = game.Workspace.Time.Hour

hour.Changed:connect(function(Property)
-- Repeat what you have above here... Besides the While wait(1) do end.



end)

EDIT: An example of what you're doing with updating the Value.

local hour = game.Workspace.Time.Hour.Value
spawn(function()
while wait(1) do
	hour = hour
    if hour== 0 or 12 then
        script.Parent.Text = "12:00"
    elseif hour== 1 or 13 then
        script.Parent.Text = "01:00"
    elseif hour== 2 or 14 then
        script.Parent.Text = "02:00"
    elseif hour== 3 or 15 then
        script.Parent.Text = "03:00"
    elseif hour== 4 or 16 then
        script.Parent.Text = "04:00"
    elseif hour == 5 or 17 then
        script.Parent.Text = "05:00"
    elseif hour== 6 or 18 then
        script.Parent.Text = "06:00"
    elseif hour== 7 or 19 then
        script.Parent.Text = "07:00"
    elseif hour== 8 or 20 then
        script.Parent.Text = "08:00"
    elseif hour== 9 or 21 then
        script.Parent.Text = "09:00"
    elseif hour== 10 or 22 then
        script.Parent.Text = "10:00"
    elseif hour== 11 or 23 then
        script.Parent.Text = "11:00"
    end
end end) 

-- Spawn allows it to loop in the background, allowing you to do more with the code. 

spawn(f)

Any other questions, be sure to leave a comment~

Good luck!

3 comments

1

Actually, you can store the value of a property in a variable, it just matters where you assign it, meaning when you get that property.

1

^ Thus needing to update it... If I'm correct. Facepalmed once I saw that, realizing I missed one point I worked to try to remember. Thanks for reminding lol.

0

Tried everything you listed here and no change. It's still as broken as ever

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