Wait() not working in functions

Hi! I’m really sorry if this has already been answered before. I looked but couldn’t find anything on this topic.

As the title suggests, “wait()” doesn’t work in functions… I think it’s functions they don’t work in, at least. I’ve run into issues that require the use of wait(), but never known how to use it since the script basically just ignores it if it’s inside of a function.

Here’s an example:

part.Touched:Connect(function(hit)
local humanoid = hit.Parent:FindFirstChild(“Humanoid”)
if humanoid ~= nil then
part.Orientation = Vector3.new(0, -15, 0)
part.Position = Vector3.new(-36.625, 4.125, -9)
wait(1) <----- That wait is always ignored
end
end)

Also, I know this is probably a stupid problem and the answer is really obvious, so sorry about that.
So far I’ve tried putting the wait() everywhere within the function, but to no avail. I’ve also looked in the forums as stated previously, once again to no avail. All I want to know is how you can use a wait() AFTER doing whatever action you want done. In this example that would be changing the orientation and position.

Thanks for any help, guys!

2 Likes

I believe you are looking for a debounce instead.

The wait doesn’t work since everytime you step on the part, a “new” script with the following function plays due to how connections work.

I’m not exactly sure what it’s called but yeah that’s how I think of it.

4 Likes

Are you looking for a debounce?

local deb = false
part.Touched:Connect(function(hit)
    if not deb then
        deb = true
            -- your code
        wait(1)
        deb = false
    end
end)
1 Like

The wait is having no effect on the rest of your script because the Touched event is creating a separate thread.

1 Like