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

how Do i make it debounce correctly and make it spawn one time?

Asked by 3 years ago

i couldnt do it

local part = game.Workspace.door
local part2 = game.Workspace.Part
local part3 = game.Workspace.Parte
local replica = game.Workspace.replic


part.touched:Connect(function(hit)
    if hit.parent:FindFirstChild("Humanoid") then
            wait(0.1)

            local imrandom = math.random(1,2)
        print(imrandom)



        if imrandom == 1 then


            local resetSeconds = 1 
            local isTouched = false

            if not isTouched then
                isTouched = true
            end

            wait(resetSeconds)
            isTouched = false

                local door = Instance.new("Part", workspace)
                door.Transparency = replica.Transparency
                door.Material = replica.Material
                door.Position = part.Position
                door.Size = replica.Size

            else
                for i = 0,1, .01 do 
                    wait()
                    part.CFrame = part.CFrame:Lerp(part2.CFrame, i)

            end



        end










        end

    end)

help

0
amazing whitespace greatneil80 2647 — 3y

1 answer

Log in to vote
0
Answered by
JesseSong 3916 Moderation Voter Community Moderator
3 years ago
Edited 3 years ago

Here's an example of how a debounce is implemented in a code. Remember that debounces are only there to prevent a code from running too many times.

Fixed Code:

local part = game.Workspace.door
local part2 = game.Workspace.Part
local part3 = game.Workspace.Parte
local replica = game.Workspace.replic
local debounce = true

part.Touched:Connect(function(hit)
    if hit.parent:FindFirstChild("Humanoid") and debounce then

        debounce = false
        wait(1) -- change to the amount of seconds you want this to elapse
        local imrandom = math.random(1,2)
        print(imrandom)



        if imrandom == 1 then


            local resetSeconds = 1 
            local isTouched = false

            if not isTouched then
                isTouched = true
            end

            wait(resetSeconds)
            isTouched = false
            debounce = true
            local door = Instance.new("Part", workspace)
            door.Transparency = replica.Transparency
            door.Material = replica.Material
            door.Position = part.Position
            door.Size = replica.Size

        else
            for i = 0,1, .01 do 
                wait()
                part.CFrame = part.CFrame:Lerp(part2.CFrame, i)

            end

        end
    end

end)

Recommendation(s):

  • Lua is case sensitive so :touchedshould be :Touched. :touched is not an event.

Sorry for not answering sooner, comment if you have more questions or replies!

Fixed minor errors!

0
change the wait() to the time you want it to run. JesseSong 3916 — 3y
Ad

Answer this question