I want to make a kill brick that decreases the players heath slowly. I try to script the killing but the first part of heath screws up, when I tested it my heath went back and forward between 90 and 80.
I want the heath to decrease not going back and forward, why is it doing this?
my script: function onTouched(part) local h = part.Parent:findFirstChild("Humanoid") if h~=nil then h.Health = 90 wait(5) h.Health = 80 wait(5) h.health = 70 wait(5) h.health = 60 wait(5) h.health = 30 wait(5) h.health = 15 wait(5) h.health = 7 wait(5) h.health = 0 end end script.Parent.Touched:connect(onTouched)
I don't know why its not working correctly
Please help me scriptinghelpers!
[Edited - I can't believe I didn't realize it I derped]
Your problem is that you need a debounce, otherwise the script will keep resetting everytime the player moves or 'bounces' on the part, and because of roblox physics, this 'bounceing' happens quite often; therefore, debounce.
db = false touch = nil script.Parent.Touched:connect(function(hit) if db == false then db = true end h = hit.Parent:FindFirstChild("Humanoid") if (h ~= nil) then touch = true while touch == true do h.Health = h.Health - 10 wait(5) end script.Parent.TouchEnded:connect(function() touch = false end) end end)
This would pretty much do the same thing as yours, debounce aside, just stop if they stopped touching the part and it reduces the health instead of setting it.
-Goulstem
What's going on is that the Touched
method of script.Parent
is getting fired multiple times, so the function is being called multiple times, making it look like the health is "hiccuping" back and forth.
To fix this, I'm going to add a 'Dying' tag to the Humanoid, and check for it before doing damage:
function onTouched(part) local h = part.Parent:FindFirstChild("Humanoid") -- lowercase f `findFirstChild` is deprecated if h and not h:FindFirstChild("Dying") then -- `~= nil` is never necessary. Instance.new("IntValue", h).Name = "Dying" h.Health = 90 wait(5) h.Health = 80 wait(5) h.health = 70 wait(5) h.health = 60 wait(5) h.health = 30 wait(5) h.health = 15 wait(5) h.health = 7 wait(5) h.health = 0 end end script.Parent.Touched:connect(onTouched)