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)
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)
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
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)
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)
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)