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
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)
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.
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
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
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.
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)
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)
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â.