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

How to use debounce??

Asked by 10 years ago

I'm using "enabled" instead of debounce. The problem here is that the weapons are spawning two or three at a time into the player's backpack. How do I fix this?

--Aceta
local venom = game.Lighting.Venomshank
local dark = game.Lighting.Darkheart
local katana = game.Lighting.BlueKatana
local sword = game.Lighting.Sword
local enabled = false

function onTouch(hit)
    if hit.Parent and hit.Parent:FindFirstChild('Humanoid') and enabled == false then
        enabled = true
        local player = game.Players:GetPlayerFromCharacter(hit.Parent)
        local character = hit.Parent
        character.Torso.CFrame = CFrame.new(Workspace.teleportation.Position+Vector3.new(math.random(1,10),math.random(1,10),math.random(1,10)))
        local Sword = sword:Clone()
        Sword.Parent = player.Backpack

        if player.Venomshank.Value == true then
            local Venom = venom:Clone()
            Venom.Parent = player.Backpack
        end

        if player.Darkheart.Value == true then
            local Dark = dark:Clone()
            Dark.Parent = player.Backpack
        end

        if player.BlueKatana.Value == true then
            local Katana = katana:Clone()
            Katana.Parent = player.Backpack
        end
        enabled = false
    end
end
script.Parent.Touched:connect(onTouch)

3 answers

Log in to vote
2
Answered by
ipiano 120
10 years ago

Personally, I wouldn't use debounce for this. If multiple people touch the block at once, only one will get the weapons because debounce is activated, and the onTouch only registers right when they touch it, not the whole time until they leave.

That being said, your problem is that sometimes onTouch registers a couple times when the parts come in contact. What I would do is check the Backpack to see if they have the weapons, and if they don't the give them.

Ad
Log in to vote
1
Answered by 10 years ago

What ipiano said regarding this specific problem is dead on. However, here is an explanation on debounces:

A debounce should be a boolean (meaning it can either be true or false but nothing else). You can choose whether to default this boolean to either true or false, but keep in mind what you've chosen!

When a certain function is called, you'd check if your debounce is NOT the default value. If it isn't, you just return right out of your function - it shouldn't do anything right now! As soon as you've checked your debounce, you should set your debounce to the opposite of it's default value, and then, at the last line of a function (or right before a return), you should set it back.

An example:

local debounce = false --Create a debounce with a default value of false

function checkDebounce() --Create a function
    if debounce then return end --Is the debounce currently true?  Okay, then just stop this function dead in its tracks!
    debounce = true --The debounce wasn't true yet, but now it is.  We don't want to overlap!
    wait(2) --Replace this with everything your function does
    debounce = false --Okay, reset the debounce!
end

script.Parent.Touched:connect(function(hit)
    checkDebounce()
end)
Log in to vote
0
Answered by 10 years ago

I've already tried checking if the tool already exists in the backpack, but it keeps erroring. After that I went to booleans.

0
How did you check for the tool? ipiano 120 — 10y

Answer this question