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

Cool down for Touched Event?

Asked by 4 years ago
Part.Touched:Connect(function(hit)
    local Hit = hit.Parent:FindFirstChild("Humanoid")
    if Hit then
        local player = game.Players:GetPlayerFromCharacter(hit.Parent)
        if player then
            if player.OtherInfo.Backpack.Value < player.OtherInfo.MaxBackpack.Value then
                Gui:TweenPosition(
                    UDim2.new(0.25, 0,0.01, 0),
                    "Out",
                    "Sine",
                    .5,
                    false
                )
                wait(5)
            end
        end
    end
end)

I have tried this but it doesn't work. What I want to do is when the player touches a part it drops down a gui but when they close and go of the part then it opens again because it detects the player touching it. Does anyone know how to fix this?

1 answer

Log in to vote
1
Answered by
SnowieDev 171
4 years ago

You should use a boolean debounce.

local HasbeenTouched = false -- Your debounce

Part.Touched:Connect(function(hit)
    local Hit = hit.Parent:FindFirstChild("Humanoid")
    if Hit then
        local player = game.Players:GetPlayerFromCharacter(hit.Parent)
        if player then
            if player.OtherInfo.Backpack.Value < player.OtherInfo.MaxBackpack.Value then
                if HasbeenTouched == false then
                    HasbeenTouched = true
                    Gui:TweenPosition(
                    UDim2.new(0.25, 0,0.01, 0),
                    "Out",
                    "Sine",
                    .5,
                    false
                    )
                    wait(5)
                    HasbeenTouched = false
                end
            end
        end
    end
end)
Ad

Answer this question