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

I don't understand what this Debounce script actually does. Any help?

Asked by 6 years ago

This is my script. I got it from another question but i don't know what it means. Thanks for help

local Player = game.Players.LocalPlayer
local char = Player.Character or Player.CharacterAdded:Wait();
local UIS = game:GetService("UserInputService")

local attacks = {
    [Enum.KeyCode.Q] = function()
        local Part = Instance.new("Part")
        Part.Parent = workspace
        Part.CFrame = char.Head.CFrame * CFrame.new(0,0,-5)
        wait(3)
    end;
    [Enum.KeyCode.E] = function()
        local Part = Instance.new("Part")
        Part.Parent = workspace
        Part.CFrame = char.Head.CFrame * CFrame.new(0,0,-5)
        wait(3)
    end;
}
local function ForceDebounce(func)
    local debounce = false;
    return function(...)
        if(not debounce) then
            debounce = true;
            func(...);
            debounce = false;
        end
    end
end
local function ForceDebounceAllAttacks()
    for i, v in pairs(attacks) do
        attacks[i] = ForceDebounce(v);
    end
end

ForceDebounceAllAttacks();
UIS.InputBegan:Connect(function(Input)
    for i, v in pairs(attacks) do
        if(Input.KeyCode == i) then
            print(i, v);
            v();
        end
    end
end)

3 answers

Log in to vote
1
Answered by
xAtom_ik 574 Moderation Voter
6 years ago

Why would you copy code if you don't understand it?

Anyway, this code stores attacks in a table. The key is the keycode and the value is the function which runs when the attack is activated. After that, it loops through the attacks and it changes their function to a debounce system.

Now, the debounce system has a local variable called "debounce". This variable will be toggled on when the attack runs, so it can not be run twice while it is running. After the attack, it is turned off so the attack can run again.

The ForceDebounce function returns another function which handles this debounce system, so if you run the returned value, it will be using the debounce system.

The final bit of code checks for the key presses and then runs the debounce function if it is the correct keycode.

So basically, in a nutshell: - Stores attacks - ForceDebounce overwrites functions to make sure you can't run the attack while it is running - Checks for inputs and runs the ForceDebounce function

Hope I helped. :)

Ad
Log in to vote
0
Answered by 6 years ago

I'm not the master, but I think it switches debounce true then false when it is false. I don't know what debounce does, but this is just my theory.

Log in to vote
0
Answered by 6 years ago

Normally Debounce is used to make sure a function is only run once per like action

What I think that debounce does in your script is just to wait until the attacks wait in the table is over before you can do another attack.

local Player = game.Players.LocalPlayer
local char = Player.Character or Player.CharacterAdded:Wait();
local UIS = game:GetService("UserInputService")

local attacks = {
    [Enum.KeyCode.Q] = function()
        local Part = Instance.new("Part")
        Part.Parent = workspace
        Part.CFrame = char.Head.CFrame * CFrame.new(0,0,-5)
        wait(3) --Attacks Wait
    end;
    [Enum.KeyCode.E] = function()
        local Part = Instance.new("Part")
        Part.Parent = workspace
        Part.CFrame = char.Head.CFrame * CFrame.new(0,0,-5)
        wait(3) --Attacks Wait
    end;
}
local function ForceDebounce(func)
    local debounce = false; -- Makes a variable named "debounce"
    return function(...) 
        if(not debounce) then
            debounce = true; -- Sets the variable to true
            func(...);
            debounce = false; -- Sets it to false
        end
    end
end
local function ForceDebounceAllAttacks()
    for i, v in pairs(attacks) do
        attacks[i] = ForceDebounce(v);
    end
end

ForceDebounceAllAttacks();
UIS.InputBegan:Connect(function(Input)
    for i, v in pairs(attacks) do
        if(Input.KeyCode == i) then
            print(i, v);
            v();
        end
    end
end)

Where it checks exactly I don't know because I am still very new to scripting, well advanced scripting, and if anyone else knows how to explain this better please do because I would like to know as well how all this work.

Answer this question