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
4 years ago
Edited 4 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:

01--[ Variables ]--
02local replicatedStorage = game:GetService("ReplicatedStorage")
03local Remotes = replicatedStorage:WaitForChild("Remotes")
04local SwingRemote = Remotes:WaitForChild("Swing")
05local ShovelData = require(game:GetService("ServerScriptService"):WaitForChild("Modules"):WaitForChild("ShovelData"))
06local replicatedData = replicatedStorage:FindFirstChild("replicatedData")
07local NPCFolder = game.Workspace:FindFirstChild("NPC's")
08 
09local RAYCAST_HITBOX = require(replicatedStorage.Modules.RaycastHitboxV2)
10 
11local CanDamage = true
12 
13--[ Main ]--
14SwingRemote.OnServerEvent:Connect(function(Player)
15    if CanDamage == true then
View all 44 lines...

1 answer

Log in to vote
0
Answered by
Necro_las 412 Moderation Voter
4 years ago
Edited 4 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:

01local function disconnectFunction(connection)
02    if connection then
03        connection:disconnect()
04    end
05end
06 
07local connection
08connection = part.Touched:Connect(function()
09    -- do stuff
10    -- connection:disconnect() wont work here
11    disconnectFunction(connection)
12end)
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 — 4y
Ad

Answer this question