Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 2 years ago
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

Log in to vote
0
Answered by 2 years ago
Edited 2 years ago
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!

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 sebastian1102 11 — 2y
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 tgarbrechtALTacc 20 — 2y
0
will fire at the same time. I'm going to re-respond to this again. tgarbrechtALTacc 20 — 2y
Ad

Answer this question