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!