Search code examples
robloxluau

Roblox Functions are running 20+ times


I seem to be having an issue of every time I want to pass a function when an event happens such as a Humanoid.Died() or a BindableEvent it runs the function 20+ times, also adding to an IntegerValue that many times as well. I have tried debounces, BindableEvents, connection:Disconnect(), etc. Is there something I'm doing wrong or missing?

Updated original code:

function module.damageHumanoid(attacker, player, humanoid, damage)
    local connection
    local debounce = false
    connection = humanoid.Died:Connect(function()
        if not debounce then
            debounce = true
            connection:Disconnect()
            module.onDeath(player)
        end
    end)
    

    if player:FindFirstChild("damage") == nil then
        newFolder = Instance.new("Folder")
        newFolder.Name = "damage"
        newFolder.Parent = player
    end

    if newFolder:FindFirstChild(tostring(attacker)) == nil then
        record = Instance.new("IntValue")
        record.Name = tostring(attacker)
        record.Parent = newFolder
    end

    if record.Value >= 100 then -- sanity check

    else
        record.Value = record.Value + damage
    end

    return humanoid:TakeDamage(damage)  

end

Which triggers this code:

function module.onDeath(player)
if player:FindFirstChild("damage") ~= nil then
    local folder = player:WaitForChild("damage")
    local contents = folder:GetChildren()

    for i,v in pairs(contents) do
        local item = folder[tostring(v)]
        damages[item.Name] = item.Value
    end
       
end

local debounce = false
if not debounce then
    debounce = true
    module.handleKill(damages)
end
end

This is the sword script's function:

function Blow(Hit)
    if not Hit or not Hit.Parent or not CheckIfAlive() or not ToolEquipped then
        return
    end
    local RightArm = Character:FindFirstChild("Right Arm") or Character:FindFirstChild("RightHand")
    if not RightArm then
        return
    end
    local RightGrip = RightArm:FindFirstChild("RightGrip")
    if not RightGrip or (RightGrip.Part0 ~= Handle and RightGrip.Part1 ~= Handle) then
        return
    end
    local character = Hit.Parent
    if character == Character then
        return
    end
    local humanoid = character:FindFirstChildOfClass("Humanoid")
    if not humanoid or humanoid.Health == 0 then
        return
    end
    local player = Players:GetPlayerFromCharacter(character)
    if player and player == Player then
        return
    end
    UntagHumanoid(humanoid)
    TagHumanoid(humanoid, Player)
    module.damageHumanoid(plur, player, humanoid, Damage)
end

Solution

  • So I guess the way I was trying to use debounce isn't correct anymore. I referred to the Roblox documentation and found this: https://create.roblox.com/docs/scripting/scripts/debounce

    If anyone would like to know what ended up working for me here it is:

        humanoid.Died:Connect(function()
            if not humanoid:GetAttribute("Killed") then
                humanoid:SetAttribute("Killed", true)
                module.onDeath(player)
                task.wait(10)
                humanoid:SetAttribute("Killed", true)
            end 
        end)