How can I simplify this code?

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)

-- sets initial statistical texts on spawn
statsUI.Cash.Text = "Cash $"..playerstats.Cash.Value
statsUI.Bank.Text = "Bank $"..playerstats.Bank.Value

Any help would be appreciated, so thanks in advance!

2 Likes

As far as I can see, there’s a couple changes that could be made.

This can be removed. It doesn’t really do anything.

You have the player’s current health passed from HealthChanged but aren’t using it.

hum.HealthChanged:Connect(function(currhealth)
	healthUI.Percentage.Text = currhealth.."%"

	-- changes color to red if player is below/equal 40hp
	healthUI.Title.TextColor3 = currhealth <= 40 and Color3.fromRGB(131, 0, 0) or -- your Color3 value here
	healthUI.Percentage.TextColor3 = currhealth <= 40 and Color3.fromRGB(131, 0, 0) or -- your Color3 value here
end)

This should be the MaxHealth, not the player’s current health.
healthUI.Percentage.Text = hum.MaxHealth.."%"

If you really wanted to, you could replace the concatenation with string formatting. For example,

healthUI.Percentage.Text = string.format("%d%%", currhealth)
2 Likes

Alright, I implemented some of the things you sent such as using currHealth, removing the if character statement, and the string formatting. I didn’t replace .Health with .MaxHealth, because my UI is intended to show the player their current health. Still, I feel like my code is a bit redundant, and I was wondering if I could just replace all the :GetPropertyChangedSignal() events with while task.wait() do instead. Would this be more efficient while also making the code shorter? Thanks in advance.

Utilizing loops rather than event-driven programming would do the opposite. The loop would be constantly running the instructions every X amount of times per second even if there’s been no change, which is the biggest advantage of event-driven programming: they trigger when something happens.

Unless the code is ran some time after the player’s character was already loaded (for some reason) I don’t see why MaxHealth would not be used. Just seems like a strange decision.

2 Likes

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