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

Why is my script firing the remote event so many times?

Asked by 2 years ago

My script fires a remote event more than once and I don't know why!

local e = nil
local cooldown = true
local RS = game:GetService("ReplicatedStorage")
local Mouse = script.Parent.Parent.Parent:GetMouse()
script.Parent.Equipped:Connect(function()
    e = true
    Mouse.Icon = "rbxasset://textures/GunCursor.png"
    script.Parent.Unequipped:Connect(function()
        e = false
        Mouse.Icon = "rbxasset://SystemCursors/Arrow"
    end)
    Mouse.Button1Up:Connect(function()
        if e and cooldown == true then
            local cooldown = false
            local sus = Mouse.Target
            if sus == "Head" or "Left Arm" or "Left Leg" or "Right Arm" or "Right Leg" or "Tail" or "Torso" then
                RS.dogedamageclone:FireServer(sus)
            end
            wait(2.5)
            local cooldown = true
        end
    end)
end)

I've done many things in the script, but I posted the whole thing because something might be messing up the end of the script.

0
The variable "cooldown" was me trying to fix the script by the way, NeoDogez 18 — 2y

1 answer

Log in to vote
0
Answered by
Shawnyg 4330 Trusted Badge of Merit Snack Break Moderation Voter Community Moderator
2 years ago
Edited 2 years ago

Your script fires more than once after unequipping/re-equipping the tool. The reason is that you have the Button1Up connection within the Equipped event, but you never disconnect it! Disconnecting the connection will prevent it from running again. You have two ways to approach this. Either put the Button1Up outside of the Equipped connection and check if the tool is equipped anytime you connect it, or leave it as is but properly manage its disconnect. For memory purposes, I'll stick to the way you made it just to show you were on the right track:

local e = nil
local cooldown = true
local RS = game:GetService("ReplicatedStorage")
local Mouse = script.Parent.Parent.Parent:GetMouse()
script.Parent.Equipped:Connect(function()
    e = true
    Mouse.Icon = "rbxasset://textures/GunCursor.png"
    local unequipConnection = nil -- This variable is used to hold the unequipped connection
    local button1UpConnection = nil -- used to hold the button1up connection
    unequipConnection = script.Parent.Unequipped:Connect(function() -- set the variable to the connection
        e = false
        Mouse.Icon = "rbxasset://SystemCursors/Arrow"

        if button1UpConnection then
            button1UpConnection:Disconnect() -- Disconnect the button1up
            button1UpConnection = nil
        end
        unequipConnection:Disconnect() -- Dsconnect the unequip
        unequipConnection = nil
    end)
    button1UpConnection = Mouse.Button1Up:Connect(function() -- Set the button1UpConnection variable to be the actual connection
        if e and cooldown == true then
            local cooldown = false
            local sus = Mouse.Target
            if sus == "Head" or "Left Arm" or "Left Leg" or "Right Arm" or "Right Leg" or "Tail" or "Torso" then
                RS.dogedamageclone:FireServer(sus)
            end
            wait(2.5)
            local cooldown = true
        end
    end)
end)
0
@Shawnyg Thanks for the help! :) NeoDogez 18 — 2y
Ad

Answer this question