#how do i make it so that damage and leaderstats dosent trigger multiple times?

10 messages · Page 1 of 1 (latest)

charred gorge
#

working on a gun script. everything works, except for the damage being triggered a few times, and by extension, leaderstats.

#
script.Parent.EquipHandler.OnServerEvent:Connect(function()
    script.Parent["Gun equip "]:play()
end)
script.Parent.FIREEEE.OnServerEvent:Connect(function(player, Hit, tool, lookVector)
    if debounce then
        print "bro tried to fire too soon"
    else
        debounce = true
        print "debounce works YAYAYAYYAAYYAYAYA"
        print "createbullet was ran"
        print (player.Name)
        local bullet = Instance.new("Part")
        bullet.Size = Vector3.new(0.2, 1, 0.2)
        bullet.BrickColor = BrickColor.new("Bright yellow")
        bullet.Material = Enum.Material.Brick
        bullet.Anchored = false
        bullet.CanCollide = false
        bullet.Position = tool.Handle.Position



        local bodyVelocity = Instance.new("BodyVelocity")
        bodyVelocity.Parent = bullet
        bodyVelocity.Velocity = Hit.LookVector * 200
        bodyVelocity.MaxForce = Vector3.new(4000, 4000, 4000)

        bullet.CFrame = tool.Handle.CFrame * CFrame.new(-2, 0, 0)

        bullet.Parent = workspace
        script.Parent["Gun Shot"]:play()
        game:GetService("Debris"):AddItem(bullet, 5)
        
        task.wait(1)
        debounce = false
        
        bullet.Touched:Connect(function(Hit)
            local humanoid = Hit.Parent:FindFirstChild("Humanoid")
            if Hit.Parent == player.Character then
                print "bro shot himself"
            elseif humanoid then
            humanoid:TakeDamage(25)
            local leaderstats = player:WaitForChild("leaderstats")
            local kills = leaderstats:WaitForChild("Kills")
            kills.Value = kills.Value + 1
                end
                bullet:Destroy() -- Destroy the bullet on impact
                if not bullet then 
                    bullet:Destroy()
                end
            end)
        end
    end)```
#

i tried some stuff using local debounce and none of it worked

granite lance
#

it’s because the bullet gets touched multiple times 💔

#

use raycasts bro

#

if you really wanna use touched tho then create a table of targets and when you hit someone insert the character into the table

#

then before you hit someone check if their character is in the table

#

but with raycasts it can only hit one thing so it doesn’t matter

crude moonBOT
#

studio** You are now Level 1! **studio

granite lance
#

and they are more reliable