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

debounce-ed function is double firing, how to fix?

Asked by 4 years ago

I have an OnTouch function for a teleporter and the debounce works but the function still fires twice. here is the code if you want to see it, its messy

par = script.Parent
local debounce = false
local Workspace = game.Workspace
function doani(player)
        --just a tween animation, removed to make the code less messy
end
par.Touched:Connect(function(part)

    if debounce then return end
    if part.Parent:FindFirstChild("Humanoid") then
        debounce = true
        local player = game.Players:GetPlayerFromCharacter(part.Parent)
        local partToTp = part.Parent:FindFirstChild("Humanoid").Parent:FindFirstChild("HumanoidRootPart")
        local Destination = par.Parent.Parent["Portal A"].portal
        par.Woosh:Play()
        spawn(function()
            doani(player)
        end)
        wait(0.7)
        partToTp.CFrame = (Destination.CFrame * CFrame.new(0,0,7))
        wait(0.6)
        debounce = false
    end
end)

Answer this question