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

NPC Damage Gets Multiplied Every Hit? (RaycastHitboxV2 Module) [SOLVED]

Asked by
0Vyp 14
3 years ago
Edited 3 years ago

Hi, so I am making a game and I was using the raycasthitboxv2 module for the melee hitbox. It works good and all except for the fact that the damage gets multiplied everytime the player swings the weapon. For example the base damage is 5 and if a player swings twice but the first time the swing didnt hit the second time if it did it then the NPC would get damaged for 10 instead of 5, and it just keeps going up by 5 every swing. Any help is appreciated. Heres my code:

--[ Variables ]--
local replicatedStorage = game:GetService("ReplicatedStorage")
local Remotes = replicatedStorage:WaitForChild("Remotes")
local SwingRemote = Remotes:WaitForChild("Swing")
local ShovelData = require(game:GetService("ServerScriptService"):WaitForChild("Modules"):WaitForChild("ShovelData"))
local replicatedData = replicatedStorage:FindFirstChild("replicatedData")
local NPCFolder = game.Workspace:FindFirstChild("NPC's")

local RAYCAST_HITBOX = require(replicatedStorage.Modules.RaycastHitboxV2)

local CanDamage = true

--[ Main ]--
SwingRemote.OnServerEvent:Connect(function(Player)
    if CanDamage == true then
        CanDamage = false
        local playerCurrentShovel = replicatedData:WaitForChild(Player.UserId).CurrentShovel.Value
        local Character = Player.Character or Player.CharacterAdded:Wait()
        local Shovel = Character:WaitForChild(playerCurrentShovel)
        local DamageToTake = ShovelData[playerCurrentShovel].Damage

        local newHitbox = RAYCAST_HITBOX:Initialize(Shovel.Handle)

        newHitbox:HitStart()
        doDamage(Shovel, newHitbox)
        wait(.1)
        newHitbox:HitStop()
        CanDamage = true    
    end
end)

function doDamage(Shovel, HitBox)
    local Character = Shovel.Parent
    local Player = game.Players:GetPlayerFromCharacter(Character)
    local playerCurrentShovel = replicatedData:WaitForChild(Player.UserId).CurrentShovel.Value
    local DamageToTake = ShovelData[playerCurrentShovel].Damage

    HitBox.OnHit:Connect(function(hit, humanoid)
        if hit.Parent ~= Character and hit.Parent.Parent == NPCFolder then
            local hitNpc = hit.Parent.Humanoid
            hitNpc:TakeDamage(DamageToTake)
        end
    end)
end

1 answer

Log in to vote
0
Answered by
Necro_las 412 Moderation Voter
3 years ago
Edited 3 years ago

Every time you call the function doDamage you create a new HitBox.OnHit connection. So first there is one connection, second there is 2 connections and so on. To prevent this check if is already connected before connecting, or try to put the conection outside the function, or disconnect when the function ends. The best way to disconnect a rbxsignal from inside is creating a function outside that when called inside will disconnect it. Like:

local function disconnectFunction(connection)
    if connection then
        connection:disconnect()
    end
end

local connection 
connection = part.Touched:Connect(function()
    -- do stuff
    -- connection:disconnect() wont work here
    disconnectFunction(connection)
end)
0
Thank you so much, I didn't realize that it was making a new connect function everytime. I thought it was just one. I guess you learn something everyday, I didn't even know you could disconnect connect functions thats really cool. 0Vyp 14 — 3y
Ad

Answer this question