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

Why is this function running over 30 times? Makes no sense.

Asked by 5 years ago

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)



0
maybe add a debounce at line 34 terence404 19 — 5y

3 answers

Log in to vote
1
Answered by
T1mes 230 Moderation Voter
5 years ago
Edited 5 years ago

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.

0
I added a debounce and tried that table thing but it added the player to the table 30 times, it literally ignored my debounce. Instead, I removed the .Died function and changed it to a conditional statement that checks if the Zombies health is 0 and it works great now. Thanks for the help though :D YabaDabaD0O 505 — 5y
Ad
Log in to vote
0
Answered by 5 years ago

You need to add a debounce at line 34 that takes up the respawn time.

Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

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.

0
Then there is no way for me to get the humanoid of the zombie the laser has hit.. YabaDabaD0O 505 — 5y
0
not true. local hit = laser.Touched:wait() local hum = hit.Parent:FindFirstChildOfClass("Humanoid") The event has no reason to fire multiple times. CorruptScript 86 — 5y

Answer this question