Firing Even though the tool is unequipped

So whenever you have a tool equipped, and you press the left mouse button, it plays an animation, but after you unequip it, pressing the mouse button still plays the animation.

local Animate = game:GetService(“ReplicatedStorage”):WaitForChild(“Animate”)
local UIS = game:GetService(“UserInputService”)

script.Parent.Parent.Parent.Equipped:Connect(function()
	UIS.InputBegan:Connect(function(Input)
		if Input.UserInputType == Enum.UserInputType.MouseButton1 then
			if script.Parent.Parent.Parent.Equipped then
				local Animate = game:GetService("ReplicatedStorage"):WaitForChild("Animate")
				Animate:FireServer(4492360012)
			end
		end
	end)
end)

All help is appreciated

2 Likes

because it connect and dont disconnect

how do I disconnect it? I tried using :Disconnect but it doesn’t work

how did you tried it
it must be used, that you put the event to var so

local event = UIS.InputBegan:Connect(function(Input)
	if Input.UserInputType == Enum.UserInputType.MouseButton1 then
		if script.Parent.Parent.Parent.Equipped then
			local Animate = game:GetService("ReplicatedStorage"):WaitForChild("Animate")
			Animate:FireServer(4492360012)
		end
	end
end)

and when you want to disconnect you will do

event:Disconnect()
local Animate = game:GetService("ReplicatedStorage"):WaitForChild("Animate")
local UIS = game:GetService("UserInputService")

local event = UIS.InputBegan:Connect(function(Input)
	if Input.UserInputType == Enum.UserInputType.MouseButton1 then
		if script.Parent.Parent.Parent.Equipped then
			local Animate = game:GetService("ReplicatedStorage"):WaitForChild("Animate")
			Animate:FireServer(4492360012)
		end
	end
end)

script.Parent.Parent.Parent.Unequipped:Connect(function()
	event:Disconnect()
end)

now if I equip it, it won’t play the animation again

you must connect the event every time you equip, so do

local event
--what is in original wersion to connect the event
script.Parent.Parent.Parent.Unequipped:Connect(function()
event:Disconnect()
end)

it still doesn’t work:

local Animate = game:GetService("ReplicatedStorage"):WaitForChild("Animate")
local UIS = game:GetService("UserInputService")

local Event
local Event = UIS.InputBegan:Connect(function(Input)
	if Input.UserInputType == Enum.UserInputType.MouseButton1 then
		if script.Parent.Parent.Parent.Equipped then
			local Animate = game:GetService("ReplicatedStorage"):WaitForChild("Animate")
			Animate:FireServer(4492360012)
		end
	end
end)

script.Parent.Parent.Parent.Unequipped:Connect(function()
	Event:Disconnect()
end)

so 1) you are redeclaring Event
2)

local event
script.Parent.Parent.Parent.Equipped:Connect(function()
	event = UIS.InputBegan:Connect(function(Input)
		if Input.UserInputType == Enum.UserInputType.MouseButton1 then
			if script.Parent.Parent.Parent.Equipped then
				local Animate = game:GetService("ReplicatedStorage"):WaitForChild("Animate")
				Animate:FireServer(4492360012)
			end
		end
	end)
end)
script.Parent.Parent.Parent.Unequipped:Connect(function()
	event:Disconnect()
end)

i know i cant describe
@VegetationBush, is it working?

its not working, i t fires even thought the tool is not equipped

and is there any error???

yes:
11:05:14.280 - Players.TOP_Crundee123.Backpack.Test.Handle.HitBox.Animation:15: attempt to index upvalue ‘Event’ (a nil value)

what line???

the Disconnect line 30chars…

@VegetationBush, can you post me whole line, because i think the problem is, that you spelled event with E except of e

If you try disconnecting an event that doesn’t exist then that error will be thrown. You should check if a given variable is in existence and if typeof(event) == "RBXScriptConnection" to make sure you are actually running :Disconnect() on an event. Add prints to validate that the code is actually running.

If this is a tool you have to understand that “Equipped” when referring to a tool is infact an Event and not property telling you whether the tool is equipped or not.

Are you possibly listening to the wrong events or tool?

Here is a working snippet:

local Tool = script.Parent
local UserInputService = game:GetService("UserInputService")
local Event

Tool.Equipped:Connect(function() 
	Event = UserInputService.InputBegan:Connect(function(InputObject, Processed) 
		print(InputObject.KeyCode)	
	end)
end)

Tool.Unequipped:Connect(function()
	if Event and typeof(Event) == "RBXScriptConnection" then
		Event:Disconnect()
	end
end)
3 Likes

No spelling error, just autocorrect from iPad
event:Disconnect()

You don’t need need the .Equipped function at the top, try removing it.