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

How to make this so the player has to touch the object for a certain amount of time before it works?

script.Parent.Touched:Connect(function(hit)
	local humanoid = hit.Parent:FindFirstChild("Humanoid")
	if humanoid ~= nil then 
		hit.Position = Vector3.new(-18, 0.5, -9)
	end
	
end	)

I want to make sure that when the player touches this object (Door) on accident they don't get teleported out.

I'm probably going to end up utilizing a while loop but I don't know what my condition would be exactly to check time.

1 answer

✓ Accepted answer
local touched = 5 -- Number of times before it works.
local debounce = false
local debouncetime = 0.5 -- .Touched will fire repeatedly, this will cool it down for a second.

script.Parent.Touched:Connect(function(hit)
	if debounce == false then
		local humanoid = hit.Parent:FindFirstChild("Humanoid")
		if humanoid ~= nil then 
			hit.Position = Vector3.new(-18, 0.5, -9)
		end
		debounce = true
		coroutine.wrap(function()
			wait(debouncetime)
			debounce = false
		end)()
	end
end)

Hope this helps!

3 comments

0

Worked perfectly thank you <3! Why exactly did you decide to make another thread through a coroutine? I kind of understand why here but I'd like to cement my knowledge

0

I just realized, I didn't answer your question properly. I read the title and read it as "so the player has to touch the object a certain amount of times before it works." Sorry! I made a coroutine because of how touched events work, if you touch an object, and then have just a regular "wait" within the code, it will only handle that single touched event, and then a bunch of other touched events

0

will fire at the same time. I'm going to re-respond to this again.

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