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

How to put debounce on line 10 for 3 seconds?

Asked by 9 years ago

I have a script that needs debounce on line 10 for 3 seconds. How do I do it?

function BackwardsWorld(plr)
    local human = plr.Parent:FindFirstChild("Humanoid")
    if human ~= nil then
        human.WalkSpeed = human.WalkSpeed - 32
        local m = Instance.new("Message")
        m.Parent = game.Workspace
        m.Text = plr.Parent.Name.." has entered the backwards world!"
        wait(3)
        m:Destroy()
        -- DEBOUNCE HERE FOR 3 SECONDS
        if human ~= nil and human.WalkSpeed < 0 then
            human.WalkSpeed = 16
            local m2 = Instance.new("Message")
            m2.Parent = game.Workspace

            m2.Text = plr.Parent.Name.." has entered the regular world!"
            wait(3)
            m2:remove()

        end
    end

end


script.Parent.Touched:connect(BackwardsWorld)
0
The problem is I don't know how you want it to work. You are too broad. EzraNehemiah_TF2 3552 — 9y
0
I edited the script. I think I know what you want it to do. EzraNehemiah_TF2 3552 — 9y

1 answer

Log in to vote
1
Answered by 9 years ago

Simple add a wait which delays the script for a little in seconds. We also need the debounce itself. Debounces work by using an if statment to check if it's a certain value. If it is that value matches it changes the value so the if statement doesn't run again until the debounce is back to the original value. Bools are the most used since it only has 2 values. True or False, 1 or 0, On or Off.


Finished Product:

local debounce = false --Debounce is false

function BackwardsWorld(plr)
    local human = plr.Parent:FindFirstChild("Humanoid")
    if human and not debounce then --human and human ~= nil are the same. Also, notice the "and". This means that 2 values have to be true. Also, there is a "not". This checks if it's false or not true.
debounce = true
        human.WalkSpeed = human.WalkSpeed - 32
        local m = Instance.new("Message")
        m.Parent = game.Workspace
        m.Text = plr.Parent.Name.." has entered the backwards world!"
        wait(3)
        m:Destroy()
       debounce = false --I guess you want the script to work again here?
        if human ~= nil and human.WalkSpeed < 0 then
            human.WalkSpeed = 16
            local m2 = Instance.new("Message", workspace) --Instance.new has 2 arguments. 1. The object, 2. The Parent.

            m2.Text = plr.Parent.Name.." has entered the regular world!"
            wait(3)
            m2:remove()

        end
    end

end


script.Parent.Touched:connect(BackwardsWorld)

EDIT

New script. I think I know what you want it to do.

local debounce = false

function BackwardsWorld(plr)
    local human = plr.Parent:FindFirstChild("Humanoid")
    if human and not debounce and humanoid.WalkSpeed > 0 then --Added this so it doesn't glitch.
debounce = true
        human.WalkSpeed = human.WalkSpeed - 32
        local m = Instance.new("Message")
        m.Parent = game.Workspace
        m.Text = plr.Parent.Name.." has entered the backwards world!"
        m:Destroy()
        elseif human and human.WalkSpeed < 0 then
            human.WalkSpeed = 16
            local m2 = Instance.new("Message", workspace)
            m2.Text = plr.Parent.Name.." has entered the regular world!"
            wait(3)
            m2:remove()
        end
        wait(3)
    debounce = false --You want it HERE not on line 10.
    end

end


script.Parent.Touched:connect(BackwardsWorld)
0
It doesnt work right. If I touch the door only once, I go into the backwards world, but I go into the regular world 3 seconds after groovydino 2 — 9y
Ad

Answer this question