PlayerGui Script not working

So i made a script to show some info about the players but it doesn’t work while in the Gui but when i put it into StarterPlayerScripts it does but that cause some problems then i’m trying to figure out why it doesn’t work in the Gui there are no outputs at all

local Player = game:GetService("Players").LocalPlayer
Player.CharacterAdded:Wait()
print("PlayerLoaded")
local Gui = Player.PlayerGui.PlayerInfo
local leaderstats = Player.leaderstats
local Exp_Folder = Player.Exp_Folder
local PlayerStats = Player.PlayerStats

local Level = leaderstats.Levels
local Exp = Exp_Folder.Exp
local OverFlow_Exp = Exp_Folder.OverFlow_Exp
local Exp_Req = Exp_Folder.Exp_Req

local Defence = PlayerStats.Defence
local Health = Player.Character.Humanoid.Health
print("Starting Gui")
while true do
	Gui.Level.Text = "Level: ".. Level.Value
	Gui["Exp/ExpReq"].Text = Exp.Value.."/"..Exp_Req.Value
	Gui.Health.Text = "Health: "..Health.."/"..Player.Character.Humanoid.MaxHealth
	Gui.Defence.Text = "Defence: "..Defence.Value
	wait(0.1)
end
1 Like
local leaderstats = Player.leaderstats
local Exp_Folder = Player.Exp_Folder
local PlayerStats = Player.PlayerStats

local Level = leaderstats.Levels
local Exp = Exp_Folder.Exp
local OverFlow_Exp = Exp_Folder.OverFlow_Exp
local Exp_Req = Exp_Folder.Exp_Req

local Defence = PlayerStats.Defence

Did you check if these already exist in the game? Use :FindFirstChild() or :WaitForChild().

yea it does exist but even if i put print(“Hello World”) in the beginning it doesn’t print it then

The issue is in:

Player.CharacterAdded:Wait()

When a localScript is put inside a gui, it starts running when everything inside the game is loaded, including the character itself.
But I would recommend you to make a check whether the player character is loaded and if is not yet to wait for it if in the future there is something that interupts the charcater adding. Here is the fixed script:

local Player = game:GetService("Players").LocalPlayer
if not(Player.Character) then Player.CharacterAdded:Wait() end -- checking if the character is inside the player before waiting
print("PlayerLoaded")
local Gui = Player.PlayerGui.PlayerInfo
local leaderstats = Player.leaderstats
local Exp_Folder = Player.Exp_Folder
local PlayerStats = Player.PlayerStats

local Level = leaderstats.Levels
local Exp = Exp_Folder.Exp
local OverFlow_Exp = Exp_Folder.OverFlow_Exp
local Exp_Req = Exp_Folder.Exp_Req

local Defence = PlayerStats.Defence
local Health = Player.Character.Humanoid.Health
print("Starting Gui")
while true do
	Gui.Level.Text = "Level: ".. Level.Value
	Gui["Exp/ExpReq"].Text = Exp.Value.."/"..Exp_Req.Value
	Gui.Health.Text = "Health: "..Health.."/"..Player.Character.Humanoid.MaxHealth
	Gui.Defence.Text = "Defence: "..Defence.Value
	wait(0.1)
end

If you have question or I have a mistake feel free to reply. I hope this helps! :smiley:

thank you for the help this worked i forgot the the Gui loads after the character loaded so thank you for your help

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.