Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Why is my GUI not updating whenever the stat changes?

Asked by
Jxemes 75
6 years ago

Hello. I'm making an FPS game, and what this script below does is that the text of the "EXPLabel" will update and change whenever I earn EXP. It doesn't really update, it just stays at saying nothing. Can anyone know what I'm missing? Or any errors?

wait(6)
local player = game.Players.LocalPlayer
local playergui = player:FindFirstChild("PlayerGui")
local gui = playergui:FindFirstChild("expguiTest")

local explabel = gui:FindFirstChild("TextLabel")

function updateHud()
explabel.Text = "" ..player:FindFirstChild("playerstats"):FindFirstChild("EXPValue").. " EXP"
end

explabel.Changed:Connect(updateHud)
player:FindFirstChild("playerstats").Changed:Connect(updateHud)
0
BTW, EXPValue's parent is in playerstats. playerstat's parent is Player. Jxemes 75 — 6y
0
why do you check that explabel has changed.. it won't change you'll have to do a while true do abnotaddable 920 — 6y
0
Please don't do a loop, triggering it off an event is doable. While True loops are the BANE of good programmers Bellyrium 310 — 6y
0
Line 9, the text would be " EXPValue EXP" Because you didn't tell it to read the VALUE of EXPValue, just the pathway to it. Bellyrium 310 — 6y

1 answer

Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

Problem:

For some reason you are referencing to the state being Changed in the TextLabel instead of referencing to the actual player's data. Then you are also referencing to a Changed state in the "playerstats", Where I presume is the folder holding the Exp value.

Solution:

Simply find when the value of the player's specific data is changed. In this case would be the EXP.

(I don't know why you have a wait at the top but it may be a chunk of your script so I will leave it).

I would highly recommend using :WaitForChild() when you do not know that the particular thing you are looking for in your script will be there whether it be because of loading issues or something along the lines of a player joining the game.

Code Below:

wait(6)
local player = game.Players.LocalPlayer
local playergui = player:WaitForChild("PlayerGui") --Might throw an error if the script runs before the player's PlayerGui loads in.
local gui = playergui:WaitForChild("expguiTest")
local explabel = gui:WaitForChild("TextLabel")

Next I would add a variable to reference to the player's Data.

local stats = player:WaitForChild("playerstats") --Again I recommend the :WaitForChild() in this instance
local Exp = stats:WaitForChild("EXPValue") --Again I recommend the :WaitForChild() in this instance

Then I would fix the Changed event for the value, which is the Exp in your case. I would also take the Gui Changed event out too.

Exp.Changed:Connect(updateHud)

Lastly, I don't see anything wrong in your function other than changing to :WaitForChild() so.

function updateHud()
explabel.Text = "" ..player::WaitForChild("playerstats"):WaitForChild("EXPValue").. " EXP"
end

All together we have:

wait(6)
local player = game.Players.LocalPlayer
local playergui = player:WaitForChild("PlayerGui") --Might throw an error if the script runs before the player's PlayerGui loads in.
local gui = playergui:WaitForChild("expguiTest")
local explabel = gui:WaitForChild("TextLabel")

local stats = player:WaitForChild("playerstats") --Again I recommend the :WaitForChild() in this instance
local Exp = stats:WaitForChild("EXPValue") --Again I recommend the :WaitForChild() in this instance

function updateHud()
explabel.Text = "" ..player::WaitForChild("playerstats"):WaitForChild("EXPValue").. " EXP"
end

Exp.Changed:Connect(updateHud)

If you have any problems or still don't understand what I typed/did just let me know in the comments.

Ad

Answer this question