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

How do you make a slow kill script where it stops once they are off the brick?

I'm trying to make a slow kill script to slowly kill a person when they are on the brick, but it stops damaging the person when they step off of it. Here's the code I have, it works but it doesn't stop when they step off the brick:

function onTouched(hit)
local h = hit.Parent:findFirstChild("Humanoid")
	repeat
	 h.Health = h.Health - 0.2
	 wait(2)
	until h.Health == 0
end
	
script.Parent.Touched:connect(onTouched)

2 answers

✓ Accepted answer

BasePart:GetTouchingParts() seems promising.

local Players = game:GetService("Players")
local Debounce = .3
local Damage = 5
while wait(Debounce) do
	local Connection = script.Parent.Touched:Connect(function() end)
	for _,Object in pairs(script.Parent:GetTouchingParts()) do
		Connection:Disconnect()
		local Rig = Object:FindFirstAncestorWhichIsA("Model")
		if Rig ~= nil and Players:GetPlayerFromCharacter(Rig) ~= nil then
			local Humanoid = Rig:FindFirstChildOfClass("Humanoid")
			if Humanoid ~= nil then
				Humanoid:TakeDamage(Damage)
			end
		end
	end
end

3 comments

0

do not use while wait() do

0

@MCAndRobloxUnited I don't think you understand how this works. They would have the same functionality, and I can gaurentee you are incorrect.

0

Thanks

You can simply do this:

script.Parent.Touched:Connect(function(hit)
	local hum = hit.Parent:FindFirstChild("Humanoid")
	if hum then
		wait(2)
		hum:TakeDamage(0.2) -- Gradual damage
	end
end)

The reason why your script keeps going is because you're telling it to repeat the action even when the Humanoid stops making contact with the Part. (You should never do this.)

1 comment

0

and no, this doesn't spam the game with damage actions but if it does then add a debounce

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