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

(SOLVED) How to end while loop inside of Touched event?

I've got this script:

local part = script.Parent


part.Touched:Connect(function(h)
	
	local humanoid = h.Parent:FindFirstChild("Humanoid")
	
	while humanoid do
		
		humanoid:TakeDamage(0.3)
		wait(0.5)
		
	end
	
end)

When the part is touched, humanoid is supposed to take damage while character is touching the part, but when the touch ends, the damage taking is supposed to stop. That won't work. I've tried saying this:

part.TouchEnded:Connect(function(h)
end)

underneath that but it wouldn't work either.

I'd also like one more thing. When the part is being stood on the damage increases and I don't want that. This is not necessary but if you can do it I'd be very thankful.

Thanks ^^

3 comments

1

or break

0

May be simple, but I still dont understand ;-;

2 answers

No loop Note: Most likely be like kill function

function onTouched(hit)
        local human = hit.Parent:findFirstChild("Humanoid")
        if (human ~= nil) then
                human.Health = human.Health - 0-.3 -- Change the amount to change the damage.
wait(0.5)  -- seconds before activating IF still touching. 
        end
end
script.Parent.Touched:connect(onTouched)

Loop Note: Acts more like kill function

function onTouched(hit)
        local human = hit.Parent:findFirstChild("Humanoid")
        if (human ~= nil) then
                human.Health = human.Health - 0-.3 -- Change the amount to change the damage.
        end
end
script.Parent.Touched:connect(onTouched)

1 comment

0

Nope, didn't work. Thank you for taking time to reply though.

Hi, so, I kinda solved my problem with this, I added this script:

local part = script.Parent
local touching

part.Touched:Connect(function(h)
	
	local humanoid  = h.Parent:FindFirstChild("Humanoid")
	touching = true

	while touching == true do
		
		humanoid:TakeDamage(0.3)
		wait(0.5)
		
	end	
	
end)

part.TouchEnded:Connect(function(h)
	
	touching = false
	
end)

so I just added a variable then assigned it to true in the touched event and false in the touchended event and inside of the while loop in the touched event i put while touching == true as a condition that's it sorry for wasting your time

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