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)
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.