Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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.

1 comment

0

The variable "cooldown" was me trying to fix the script by the way,

1 answer

✓ Accepted answer

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)

1 comment

0

@Shawnyg Thanks for the help! :)

This discussion is preserved as it was. New answers, comments, and votes are closed.