Tool.Equipped not working

I’m trying to play an animation when the tool is equipped and unequipped however, nothing seems to happen. No error seems to pop up in the output box and even the print statements do not appear.

local Tool = script.Parent
local player = game:GetService('Players').LocalPlayer
local char = player.CharacterAdded:Wait()
local Hum = char:WaitForChild('Humanoid')
local animator = Hum:FindFirstChildOfClass('Animator')

local properties = char:WaitForChild('properties')

local UIS = game:GetService('UserInputService')

local katanaFolder = game:GetService('ReplicatedStorage'):WaitForChild('katanaFiles')
local animFolder = katanaFolder:WaitForChild('Animations')

local Anims = {}
for i,v in pairs(animFolder:GetChildren()) do
   if animator then
	   Anims[v.Name] = animator:LoadAnimation(v)
   end
end

local Equipped = false
local Debounce = false
local CD = .28
local longCD = .8

local currTime
local prevTime = 0
local count = 0

--

Tool.Equipped:connect(function()
   print('equipped')
   Equipped = true
   Anims['Equip']:Play()
end)

Tool.Activated:connect(function()
   UIS.InputBegan:connect(function(input)
	   if input.KeyCode == Enum.UserInputType.MouseButton1 then
		   print('hi')
	   end
   end)
end)


Tool.Unequipped:connect(function()
   print('unequipped')
   Equipped = false
   Anims['Unequip']:Play()
end)

The tool has a handle and I have enabled the requiresHandle property aswell.

1 Like

Okay, so there are a lot of funny stuff in here that you simply do not need.

If you try to remove this clutter you might be able to find the problem.

If the character has already been added this will yield forever this is most likely the problem so instead use

local char = Player.Character or Player.CharacterAdded:Wait()

You should probably include this if statement outside the loop not inside as the animator is not accessed inside but out. It will improve performance

This is a useless variable as you are using Equipped and Unequipped If you wanted to check whether or not the tool is equipped yes but in your case it would be easier to get just use the events

Try to not use deprecated code

Tool.Equipped:Connect
--Instead of
Tool.Equipped:connect

It is good practice

You are connecting inside a connection? That will cause major lag if the tool is used frequently as the connections are not disconnected after use. See RBXScriptConnection | Roblox Creator Documentation and remember to disconnect events you are not using.

Here is what I recomend

local ActivatedConnection;
Tool.Equipped:Connect(function(Mouse)
    print("Tool Equipped")
    ActivatedConnection = Tool.Activated:Connect(function()
        print("Tool Clicked at:",Mouse.Hit.Position)-- e.g. if you are going to make a ranged weapon 
    end)
end)
Tool.Unequipped:Connect(function()
    ActivatedConnection:Disconnect()
end)

User input service is a good way to detect events however I usually use it if I am not using Tools. I recommend you look into the hub.

2 Likes

Hi, thanks for all the improvements that I could make to the code :slight_smile: . I implemented the things you said I could improve on however, it still does not print anything on the output nor do anything.

1 Like

Try removing this line, your character likely has no children with the name properties. Hope this helps :slight_smile:

2 Likes

I tried it but it still doesn’t work. My character has a properties folder as onPlayerAdded on another server script.

1 Like

Try printing something after all your variables and see if it’ll print, if it prints nothing then its an issue with waiting

2 Likes

I found out printing seems to stop at this part:

local katanaFolder = game:GetService('ReplicatedStorage'):WaitForChild('katanaFiles')
local animFolder = katanaFolder:WaitForChild('Animations')

but I don’t see anything wrong with it?

Very weird, just tested your code and it worked perfectly for me.

2 Likes

Ah, I found the problem. I believe it was as I called the folder ‘katanaFIles’ with a capital I instead of lowercase i.
It seems to work now, thanks for the help though! :slight_smile:

1 Like

Ahh no problem man glad you found the issue :slight_smile:

1 Like

try this

local UIS = game:GetService("UserInputService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Tool = script.Parent
local player = game.Players.LocalPlayer

local EquipAnim = ReplicatedStorage.katanaFiles.Animations.Equip
local UnequipAnim = ReplicatedStorage.katanaFiles.Animations.Unequip
local EquipAnimTrack = player.Character.Humanoid:LoadAnimation(EquipAnim)
local UnequipAnimTrack = player.Character.Humanoid:LoadAnimation(UnequipAnim)

Tool.Equipped:connect(function()
	print('equipped')
	EquipAnimTrack:Play()
end)

Tool.Unequipped:connect(function()
	print('unequipped')
	EquipAnimTrack:Stop()
	EquipAnimTrack.Stopped:Wait()
	UnequipAnimTrack:Play()
end)

Tool.Activated:connect(function()
	UIS.InputBegan:connect(function(input)
		if input.UserInputType = Enum.UserInputType.MouseButton1 then
			print('hi')
		end
	end)
end)

btw use Input.UserInputType for like mouse stuff not Keycode

1 Like