Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

i want a kill brick to decrease heath not going back and forward, why is it doing this?[solved]

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!

1 comment

0

Please put your code inside of the two lines of squiggles (which should each be on their own line) created when click the Lua symbol on your text editor. It formats the code to make it easier to read.

2 answers

✓ Accepted answer

[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

3 comments

1

You actually fixed the problem in the code, without noticing! The debounce you added fixed why his code had the health "bouncing back and forth", but your code adds multiple additional bugs, and doesn't guarantee a kill like the OP's code eventually does.

1

Yes, I just noticed after your post lol I can't believe i missed it. But I mean I doubt theres any want for someone to definitely die even if they're off the part. I assumed, which isn't a very good thing, but I hope it was good for him(:

0

It's not a bad design choice, but it produces different results from what the OP seems to want, based on his original code.

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)

This discussion is preserved as it was. New answers, comments, and votes are closed.