I created a laser gun and so when the gun kills a zombie it gives the player +5 points, but it's giving them over 200 points for killing one zombie even though I'm only adding +5 points..
I'm using the Humanoid.Died function, I added a print and it printed "Died!" 30 times into output..
What is wrong with this? Is this a bug or something because after I kill the zombie it fires but it runs like 30 times?
--// Variables local replicatedStorage = game:GetService('ReplicatedStorage') local remotesFolder = replicatedStorage:WaitForChild('Remotes') local laserEvent = remotesFolder:WaitForChild('FireLaser') --// Main --laserEvent:FireServer(mouse.Hit.p, handle.CFrame.p) laserEvent.OnServerEvent:Connect(function(player, mousePos, handlePos) local ray = Ray.new(handlePos, (mousePos - handlePos).unit * 500) local part, pos = workspace:FindPartOnRay(ray, player, false, true) local laser = Instance.new('Part') laser.Parent = workspace laser.Anchored = true laser.BrickColor = BrickColor.Red() laser.Transparency = 0.3 laser.Material = Enum.Material.Neon laser.CanCollide = false local dist = (mousePos - handlePos).magnitude laser.Size = Vector3.new(0.3, 0.3, dist) laser.CFrame = CFrame.new(handlePos, pos) * CFrame.new(0, 0, -dist/2) laser.Touched:Connect(function(hit) local deb = false local hum = hit.Parent:FindFirstChildWhichIsA('Humanoid') if hum and hum.Parent.Name ~= player.Name then hum:TakeDamage(5) hum.Died:Connect(function() print('Died!') local killerVal = hum.Parent:FindFirstChild('Killer') if killerVal then killerVal.Value = player killerVal.Value:WaitForChild('leaderstats'):WaitForChild('Points').Value = killerVal.Value:WaitForChild('leaderstats'):WaitForChild('Points').Value + 5 end end) end wait(3) deb = true end) game:GetService('Debris'):AddItem(laser, 0) end)
It's because you're looking for the Died event every time it touches something.
So basically it's touching the zombie multiple times and looking for the died event multiple times leaving 30 times died.
I suggest adding a debounce per person. One way of doing this is making a table and adding the player to the table for the first time the player/zombie is touched.
Then add in the if statement checking if the player isn't in the table.
Hope this helps.
You need to add a debounce at line 34 that takes up the respawn time.
You're binding a new function to the death event each time the laser touches the zombie. Additionally, there's no reason to connect a function to the Touched event in the first place. Instead try using the wait method.