Guys i've been trying to do this simple script but i don't get why it doesn't work
local hum = script.Parent:FindFirstChild("Humanoid") while true do wait() print(hum.Health) end if hum.Health == 50 then hum.Health = 100 end
i added a server script into the dummy but why it doesnt work the dummy just dies
The code isn't actually getting to the if statement, it is just repeating the while true do statement
For this type of script I would use the changed function
fix:
local hum = script.Parent:FindFirstChild("Humanoid") hum.Changed:connect(function() if hum.Health <= 50 then hum.Health = 100 end end
I like where @Gnossien is taking this and I would like to have a more specific / efficient solution:
local hum = script.Parent:FindFirstChild("Humanoid") hum.HealthChanged:Connect(function(newHealth) -- The humanoid actually has a listener that awaits for HealthChange also make sure to avoid deprecated things if newHealth <= 50 then hum.Health = 100 end end
I've changed small things but it still in an improvement:
- Specifically awaiting change on a property with a provided listener
Since the Humanoid
comes with a HealthChanged
event you might as well use it rather then listening for any changes on the Humanoid. In general you want to keep your code specific
- :connect to :Connect
not much here but a lesson to be learned is to avoid deprecated methods and functions since Roblox may remove them at any moment (though it may not seem likely)
These are small changes but it's best to keep your code as clean as possible. I'm just adding on. Best of luck and I hope this helped you!