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 comment
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.