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

How do I make a "while touched" function?

I want to know how to make a "while touched" script.

So when a player is touching a brick, it will execute a function, but I also want it were the function will always execute intill the player steps off the brick.

So I want a loop when you are touching and i want the loop to end when you stop touching

thank you for any type of help!

1 answer

✓ Accepted answer
local block = script.Parent
on = true

function touching()
	print ('Touching!')
end

function notTouching()
	print ('Not Touching!')
end


block.Touched:connect(function(hit)

		if hit.Parent:FindFirstChild("Humanoid") and on == true then --we put this so if item fall in there or the part touching to the ground it will not activated
			touching()
			wait (3)
			on = false
		end
end)

block.TouchEnded:connect(function(hit)
	   if hit.Parent:FindFirstChild("Humanoid") and on == false then --same like this one
		notTouching()
		wait (3)
		on = true
	end
end)

 

i put the comparing variable so it will not spamming printing until 3 second ,sorry if i wrong im still learning too :)

1 comment

0

Your 'on' variable suffices as a debounce, no need for the extra 3 second wait. What you've written would make it so that you can't leave the part until 3 seconds after touching it, which I would imagine to be inconvenient for players. +1 for the otherwise accurate answer

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