Jaska008
Jaska008

Reputation: 1

Added points keep multiplying in Roblox

I have a script that is supposed to give 5 points for each kill. However, on the first kill it gives 0 points. On the second, it gives 5. On the third, it gives 10, and so on. I can't figure out what is causing this.

The crusher part fires an event when something dies on it. This is then picked up by another script to give the points, if the crusher's button was used during the kill. https://youtu.be/06c4KFsvIzQ

-- crusher script --

local debounce = false
script.Parent.Touched:Connect(function(OnTouched)
    if not debounce then
        debounce = true
        local hum = OnTouched.Parent:FindFirstChild("Humanoid")
        if hum ~= nil and hum.Health ~= 0 then
            hum.Health = 0
            script.GotKill:Fire()
        end
        debounce = false
    end
end)

-- Button script --

local pos = script.Parent.Parent.Parent.Part.Position
local Crusher = script.Parent.Parent.Parent.Part
local wall1= script.Parent.Parent.Parent.bobbade
local wall2 = script.Parent.Parent.Parent.bebbade
local pos2 = script.Parent.Parent.Position
local debounce = false

script.Parent.MouseClick:Connect(function(Clicked)
    if not debounce then
        debounce = true
        script.Parent.Parent.Position = pos2 - Vector3.new(0,0.5,0)
        Crusher.Position = Crusher.Position - Vector3.new(0,1,0)
        wall1.Position = wall1.Position - Vector3.new(0,1,0)
        wall2.Position = wall2.Position - Vector3.new(0,1,0)
        wait()
        Crusher.Position = Crusher.Position - Vector3.new(0,1,0)
        wall1.Position = wall1.Position - Vector3.new(0,1,0)
        wall2.Position = wall2.Position - Vector3.new(0,1,0)
        wait()
        Crusher.Position = Crusher.Position - Vector3.new(0,1,0)
        wall1.Position = wall1.Position - Vector3.new(0,1,0)
        wall2.Position = wall2.Position - Vector3.new(0,1,0)
        wait()
        Crusher.Position = Crusher.Position - Vector3.new(0,1,0)
        wall1.Position = wall1.Position - Vector3.new(0,1,0)
        wall2.Position = wall2.Position - Vector3.new(0,1,0)
        wait()
        Crusher.Position = Crusher.Position - Vector3.new(0,1,0)
        wall1.Position = wall1.Position - Vector3.new(0,1,0)
        wall2.Position = wall2.Position - Vector3.new(0,1,0)
        wait()
        Crusher.Position = Crusher.Position - Vector3.new(0,1,0)
        wall1.Position = wall1.Position - Vector3.new(0,1,0)
        wall2.Position = wall2.Position - Vector3.new(0,1,0)
        wait()
        Crusher.Position = Crusher.Position - Vector3.new(0,1,0)
        wall1.Position = wall1.Position - Vector3.new(0,1,0)
        wall2.Position = wall2.Position - Vector3.new(0,1,0)
        wait(1)
        Crusher.Position = Crusher.Position + Vector3.new(0,1,0)
        wall1.Position = wall1.Position + Vector3.new(0,1,0)
        wall2.Position = wall2.Position + Vector3.new(0,1,0)
        wait()
        Crusher.Position = Crusher.Position + Vector3.new(0,1,0)
        wall1.Position = wall1.Position + Vector3.new(0,1,0)
        wall2.Position = wall2.Position + Vector3.new(0,1,0)
        wait()
        Crusher.Position = Crusher.Position + Vector3.new(0,1,0)
        wall1.Position = wall1.Position + Vector3.new(0,1,0)
        wall2.Position = wall2.Position + Vector3.new(0,1,0)
        wait()
        Crusher.Position = Crusher.Position + Vector3.new(0,1,0)
        wall1.Position = wall1.Position + Vector3.new(0,1,0)
        wall2.Position = wall2.Position + Vector3.new(0,1,0)
        wait()
        Crusher.Position = Crusher.Position + Vector3.new(0,1,0)
        wall1.Position = wall1.Position + Vector3.new(0,1,0)
        wall2.Position = wall2.Position + Vector3.new(0,1,0)
        wait()
        Crusher.Position = Crusher.Position + Vector3.new(0,1,0)
        wall1.Position = wall1.Position + Vector3.new(0,1,0)
        wall2.Position = wall2.Position + Vector3.new(0,1,0)
        wait()
        Crusher.Position = Crusher.Position + Vector3.new(0,1,0)
        wall1.Position = wall1.Position + Vector3.new(0,1,0)
        wall2.Position = wall2.Position + Vector3.new(0,1,0)
        script.Parent.Parent.Position = pos2
        script.Parent.Parent.Parent.Part.Script.GotKill.Event:Connect(function(OnKill)
            Clicked.leaderstats.Gold.Value = Clicked.leaderstats.Gold.Value + 5
        end)
        debounce = false
    end
end)

Upvotes: 0

Views: 120

Answers (1)

Kylaaa
Kylaaa

Reputation: 7188

You are essentially adding an event listener every time you click on the button. When you simplify your Button pressed code you get this :

script.Parent.MouseClick:Connect(function(Clicked)
    -- add a listener for the GotKill signal to fire
    script.Parent.Parent.Parent.Part.Script.GotKill.Event:Connect(function(OnKill)
        -- update the score
        Clicked.leaderstats.Gold.Value = Clicked.leaderstats.Gold.Value + 5
    end)
end)

The first time you hit the button, the GotKill event probably fires before you add the listener for it, so you get zero points. Now you have one callback that will fire when the GotKill event triggers.

The second time, your first callback will pick up the signal that you have missed, you get your 5 points, then you add another listener. Now you have two callbacks listening to the signal.

The third time, your two previous callbacks fire, now you get 5 points added twice for 10 points, then you add another listener, and so on.

To fix this, move the connection to the GotKill event higher in your MouseClick function, then disconnect it when the animation ends.

-- Button Script --

local pos = script.Parent.Parent.Parent.Part.Position
local pos2 = script.Parent.Parent.Position
local Crusher = script.Parent.Parent.Parent.Part
local wall1 = script.Parent.Parent.Parent.bobbade
local wall2 = script.Parent.Parent.Parent.bebbade

local debounce = false

-- play an animation on MouseClick
script.Parent.MouseClick:Connect(function(Clicked)
    if not debounce then
        debounce = true

        -- listen for the signal to update the score
        local connection = Crusher.Script.GotKill.Event:Connect( function()
            Clicked.leaderstats.Gold.Value = Clicked.leaderstats.Gold.Value + 5
        end)

        -- play the crusher animation

        -- clean up the GotKill listener
        connection:Disconnect()

        debounce = false
    end
end)

Upvotes: 2

Related Questions