Is there a better way I can constantly check the player's stats in my GUI?

I have a basic GUI that shows you several values from the player’s stats such as Health, Stamina, Money, etc. In this GUI, I am using a LocalScript to handle all of this, and I have multiple :GetPropertyChangedSignal() events to check these stats. I am wondering if there is a greater alternative that can be used, such as while task.wait() do or something in RunService, but I’m not entirely sure.

Here’s the code:

-- services
local Players = game:GetService("Players")

-- define
local gui = script.Parent

local healthUI = gui.Health
local staminaUI = gui.Stamina
local statsUI = gui.Stats
local ftpUI = gui.FirstTimePlaying

local plr = Players.LocalPlayer
local playerstats, leaderstats, playersettings = plr:WaitForChild("playerstats"), plr:WaitForChild("leaderstats"), plr:WaitForChild("playersettings")

local char = plr.Character or plr.CharacterAdded:Wait()
local hum: Humanoid = char:WaitForChild("Humanoid")

-- show player their health and stamina
if char and hum and hum.Health > 0 then
	while not char:GetAttribute("Stamina") do task.wait() end

	-- checks when health changes
	hum.HealthChanged:Connect(function(dmg)
		healthUI.Percentage.Text = hum.Health.."%"

		if hum.Health <= 40 then -- changes color to red if player is below/equal 40hp
			healthUI.Title.TextColor3 = Color3.fromRGB(131, 0, 0)
			healthUI.Percentage.TextColor3 = Color3.fromRGB(131, 0, 0)
		end
	end)

	-- checks when stamina changes
	char:GetAttributeChangedSignal("Stamina"):Connect(function()
		staminaUI.Percentage.Text = char:GetAttribute("Stamina").."%"
	end)

	-- sets initial percentage texts on spawn
	healthUI.Percentage.Text = hum.Health.."%"
	staminaUI.Percentage.Text = char:GetAttribute("Stamina").."%"
end

-- show player their stats
playerstats.Cash:GetPropertyChangedSignal("Value"):Connect(function()
	statsUI.Cash.Text = "Cash $"..playerstats.Cash.Value
end)
playerstats.Bank:GetPropertyChangedSignal("Value"):Connect(function()
	statsUI.Bank.Text = "Bank $"..playerstats.Bank.Value
end)

statsUI.Cash.Text = "Cash $"..playerstats.Cash.Value
statsUI.Bank.Text = "Bank $"..playerstats.Bank.Value

Any feedback or help would be appreciated!

3 Likes

While task.wait() and RunService would be much worse, since it would try to update even when nothing has changed. Your current method is way better than those you listed and is what I use personally and it works fine performance wise. Word of warning though! In your health script, you never revert the health display to the original color if the player has healed back to 40 or greater, if it’s intended then ignore this, but if its not you should revise it

3 Likes

Thank you very much, I completely overlooked that issue with the health segment, and you just gave me the much needed fixation to make it better!

1 Like