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

GUI text not updating at all. Why isn't it?

I have this generator, which as a concept generates energy. I need this GUI to update to show the player how much energy they have.

This is my code:

local text = script.Parent
local display = game.Workspace.Generator.ClickButton.ClickDetector.PowerCore.Energy.Value


while wait() do
	dis = tostring(display)
	text.Text = "Total energy: " .. dis
end

I put the variable dis to update every tick so the value would change depending on how much energy the player has. It stays at 0.

7 comments

0

If the energy is 0, it should stay 0?

0

the energy goes up by 0.2 every 2 seconds, but the GUI doesn't update the value

0

Example: i have a function to print the amount of energy every second, and it counts up, while the GUI stays at 0. sorry i phrased the question wrong lol

0

Ah, its fine. Anyways is the code in a localscript or script? Make sure its enabled too.

0

the code is in a regular script. should it be in a local script?

0

Dude, why are you using tostring on a value that is already a string?

0

Also, it's going to have to be in a localscript if you're trying to access the player's playergui.

2 answers

Because youre not updating the display variable(it only gets assigned once, at the start of the script). put it inside the while true loop so it gets updated. Ill edit this answer later if it isn't sufficient since I'm using a phone It should also be in local script because playergui is invisible to server

Updated Code

local text = script.Parent
 

while wait() do
local display = game.Workspace.Generator.ClickButton.ClickDetector.PowerCore.Energy.Value --always updates with the energy value
dis = tostring(display)
text.Text = "Total energy: " .. dis
end

I recreated the situation in studio, and it works fine. Make sure to put local script INSIDE the textlabel

The reason for this is when you declare the value at the start of the script

Original Code

local text = script.Parent
local display = game.Workspace.Generator.ClickButton.ClickDetector.PowerCore.Energy.Value --only gets assigned once

while wait() do
dis = tostring(display)
text.Text = "Total energy: " .. dis
end

Suppose the starting value of energy is 3. Since the variable display only gets assigned the value once (When the game starts) The variable will keep the value 3 until it is updated again. If you put it inside the while loop like in the first given code, the variable gets updated constantly with the Energy Value

7 comments

0

No effect

0

updated

0

Still no effect, I did the same on both local scripts and regular scripts

0

did you put "display = game.Workspace.Generator.ClickButton.ClickDetector.PowerCore.Energy.Value" in the while loop?

0

try while true instead of while wait()

0

doing while true just breaks Studio

0

Updated

In your script, you write:

local text = script.Parent
local display = game.Workspace.Generator.ClickButton.ClickDetector.PowerCore.Energy.Value

while wait() do
    dis = tostring(display)
    text.Text = "Total energy: " .. dis
end

The error here, is that display = 0 forever. In other words, the value associated with the variable display does not update.

You need to place the variable display under a loop function that rechecks the value of the energy repeatedly. So to do that, you just simply move it under the while do loop function that you have already written.

local text = script.Parent

while wait() do
	local display = game.Workspace.Generator.ClickButton.ClickDetector.PowerCore.Energy.Value
    dis = tostring(display)
    text.Text = "Total energy: " .. dis
end

Problem solved.

1 comment

0

If it still does not work, make sure your variables are assigned correctly.

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