Help turning something into a function

How to turn this in to a function so it is not laggy

local Humanoid = script.Parent.Parent.Parent.Parent.Parent:FindFirstChild("Humanoid")

while true do
	task.wait(0.1)

	script.Parent.HealthNum.Text = math.floor(Humanoid.Health) .. " / " .. math.floor(Humanoid.MaxHealth) --NPC health
	
	local pie = (Humanoid.Health / Humanoid.MaxHealth)
	script.Parent.Healthbar.Size = UDim2.new(pie, 0, 1, 0) --moves the bar to the health of the NPC
end

Like this?

local Humanoid = script.Parent.Parent.Parent.Parent.Parent:FindFirstChild("Humanoid")

local function HealthText()
   script.Parent.HealthNum.Text = math.floor(Humanoid.Health) .. " / " .. math.floor(Humanoid.MaxHealth) --NPC health
	
	local pie = (Humanoid.Health / Humanoid.MaxHealth)
	script.Parent.Healthbar.Size = UDim2.new(pie, 0, 1, 0) --moves the bar to the health of the NPC
end

while true do
	task.wait(0.1)
       HealthText()
end

Also this shouldn’t increase or decrease lag. If your looking for that I recommend you use :GetPropertyChangedSignal() like this:

local Humanoid = script.Parent.Parent.Parent.Parent.Parent:FindFirstChild("Humanoid")

local function HealthText()
   script.Parent.HealthNum.Text = math.floor(Humanoid.Health) .. " / " .. math.floor(Humanoid.MaxHealth) --NPC health
	
	local pie = (Humanoid.Health / Humanoid.MaxHealth)
	script.Parent.Healthbar.Size = UDim2.new(pie, 0, 1, 0) --moves the bar to the health of the NPC
end

Humanoid:GetPropertyChangedSignal("Health"):Connect(function()
	HealthText()
end)

Humanoid:GetPropertyChangedSignal("MaxHealth"):Connect(function()
	HealthText()
end)
1 Like

This provided script is no difference in the one he provided except the fact that you put a function to it so there’s a chance perfomance might not change but I might be wrong.

Yep. Which the OP asked…

I did provide a second result for the laggy part which is likely the while true loop, by instead using :GetPropertyChangedSignal()

I myself was confused on the request

Just use Humanoid.HealthChanged, much faster and more efficient than a loop.

Why if HealthChanged Exists?

There is slight difference.
Instead of the loop which causes bad performance, he used event that detects if property has been changed so you don’t have to update the function every time when you’re using the while true do

1 Like

I liked :GetPropertyChangedSignal("Health") better and so it also allows Humanoid:GetPropertyChangedSignal("MaxHealth"). I remember that there was this post that showed :GetPropertyChangedSignal() had slightly better performance then things like HealthChanged. I’ll try and find it though

1 Like

There is no reason you should detect Property Change on something It already has a Event for, Its like the exact same thing with ValueBase.Changed with people using ValueBase:GetPropertyChangedSignal("Value") when they will both do the exact same thing except ValueBase.Changed will return the new Value, just like how HealthChanged will return the Current health of the Player, All it does is simplify the code, not trying to make a fuss about it.

Yup. Again there is no wrong way you can use them, both do the same thing. I do like :GetPropertyChangedSignal() for usage on the dev forum (Even if there is Another one) as it can be applied to mostly every instance, where .changed and .HealthChanged has some restrictions which can be confusing to people, especially if they are new to scripting. But again, I really don’t care which one people use.

1 Like

Ok which is better for lag. A or B

Script A

local Humanoid = script.Parent.Parent.Parent.Parent.Parent:FindFirstChild("Humanoid")
local function updateHealth()
script.Parent.HealthNum.Text = math.floor(Humanoid.Health) .. " / " .. math.floor(Humanoid.MaxHealth) --NPC health
local pie = (Humanoid.Health / Humanoid.MaxHealth)
script.Parent.Healthbar.Size = UDim2.new(pie, 0, 1, 0) --moves the bar to the health of the NPC
end
Humanoid:GetPropertyChangedSignal("Health"):Connect(function()
updateHealth()
end)
Humanoid:GetPropertyChangedSignal("MaxHealth"):Connect(function()
updateHealth()
end)

Script B

local Humanoid = script.Parent.Parent.Parent.Parent.Parent:FindFirstChild("Humanoid")
function updateHealth()
script.Parent.HealthNum.Text = math.floor(Humanoid.Health) .. " / " .. math.floor(Humanoid.MaxHealth) --NPC health
local pie = (Humanoid.Health / Humanoid.MaxHealth)
script.Parent.Healthbar.Size = UDim2.new(pie, 0, 1, 0) --moves the bar to the health of the NPC
end
Humanoid.HealthChanged:Connect(updateHealth)

They Both worked and did what I wanted so thank you

The B is better than the A (char limittt)

local Humanoid = script.Parent.Parent.Parent.Parent.Parent:FindFirstChild("Humanoid")
function updateHealth()
script.Parent.HealthNum.Text = math.floor(Humanoid.Health) .. " / " .. math.floor(Humanoid.MaxHealth) --NPC health
local pie = (Humanoid.Health / Humanoid.MaxHealth)
script.Parent.Healthbar.Size = UDim2.new(pie, 0, 1, 0) --moves the bar to the health of the NPC
end
Humanoid.HealthChanged:Connect(updateHealth)

They both work and both do the same thing. Not one is really ‘better’ then the other, just had 2 different outcomes for the same result.

Edit: Both won’t really effect lag which one you use. They both detect the same thing

Not really. It’s the same thing

Ya but I want the lest laggiest

Yes they do the same thing but one make your game less laggy (B),even if it’s not a lot…

The “least laggiest” ultimately depends on how you handle the Event, otherwise it should be the same as running any code.

1 Like

I what the least laggiest because I have a bunch of NPCs and they all have this code in each one of them.

Having one script per npc will make your game laggy really bad you should make one script a a for loop

Why? ok yes that can cause some lag. Not the event itself but how many scripts you will make and how your handling it, You should only need one ‘master script’.