UserInputService wont fire inside tool

My gun tool doesn’t activate when the corresponding key is pressed
The output doesn’t show anything either, I don’t get any errors when pressing keys

I couldn’t find any solutions for this

userInputService.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.UserInputType.MouseButton1 and isEquipped == true then 
		local Anim = Instance.new("Animation")
		Anim.AnimationId = "rbxassetid://16700037866"
		local track
		track = script.Parent.Parent.Humanoid:LoadAnimation(Anim)
		track.Priority = Enum.AnimationPriority.Action4
		track.Looped = false
		track:Play()
	end
end)

It should just play the animation when MouseButton1 is pressed, but it does nothing, the animation also has priority above every other animation

Where my script is:
image

local isEquipped = true -- Assuming you set this elsewhere

userInputService.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.UserInputType.MouseButton1 and isEquipped then
		print("MouseButton1 pressed")
		local Anim = Instance.new("Animation")
		Anim.AnimationId = "rbxassetid://16700037866"
		local track = script.Parent.Parent.Humanoid:LoadAnimation(Anim)
		track.Priority = Enum.AnimationPriority.Action4
		track.Looped = false
		track:Play()
		print("Animation started")
	end
end)

use Tool.Activated it is more reliable and shortens the code. And it works for both pc and mobile

This doesnt seem to do anything still, no output either

I want to add reloading/crouching etc. to the tool, i can use Tool.Activated for the firing but i need UIS for the other functions

  1. Input Service Connection:

    • Ensure that you have the following line of code to connect the userInputService:
      local userInputService = game:GetService("UserInputService")
      
  2. Check the isEquipped Variable:

    • Confirm that the isEquipped variable is correctly set to true when the gun tool is equipped. If it’s not set properly, the animation won’t play even if the MouseButton1 is pressed.
  3. Animation Loading and Playing:

    • Verify that the animation ID "rbxassetid://16700037866" is valid and corresponds to an existing animation in your game.
    • To debug, add print statements to check if the animation is being loaded correctly:
      local Anim = Instance.new("Animation")
      Anim.AnimationId = "rbxassetid://16700037866"
      local track = script.Parent.Parent.Humanoid:LoadAnimation(Anim)
      print("Animation loaded successfully")
      track.Priority = Enum.AnimationPriority.Action4
      track.Looped = false
      track:Play()
      print("Animation started playing")
      
  4. Animation Priority and Conflicts:

    • Ensure that there are no conflicting animations with higher priorities that might interrupt or override this one.
    • Check if any other animations are playing simultaneously and interfering with this one.
  5. Debugging Output:

    • Inspect the output console for any errors or warnings related to animations.
    • Print messages when the MouseButton1 is pressed:
      userInputService.InputBegan:Connect(function(input)
          if input.KeyCode == Enum.UserInputType.MouseButton1 and isEquipped then
              print("MouseButton1 pressed")
              -- Rest of your animation code
          end
      end)
      

Let me know if you have any furher issues.

I have done all of this, the animations work fine as ive tested them outside of using UIS with the same code

full code

local userInputService = game:GetService("UserInputService")
local tool = script.Parent
local plr = game.Players.LocalPlayer
local character = plr.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")

local isEquipped = false
local reloading = false
local firing = false
local crouching = false

tool.Equipped:Connect(function()
	isEquipped = true
end)

tool.Unequipped:Connect(function()
	isEquipped = false
end)

userInputService.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.UserInputType.MouseButton1 and isEquipped then
		print("MouseButton1 pressed")
		local Anim = Instance.new("Animation")
		Anim.AnimationId = "rbxassetid://16700037866"
		local track = script.Parent.Parent.Humanoid:LoadAnimation(Anim)
		track.Priority = Enum.AnimationPriority.Action4
		track.Looped = false
		track:Play()
		print("Animation started")
	end
end)

This still does not work, and wont print anything either

I did some change to help debug. But I cant get sure if the animation is playing right.

local userInputService = game:GetService("UserInputService")
local isEquipped = true -- Make sure this is set correctly

userInputService.InputBegan:Connect(function(input)
	if input.UserInputType == Enum.UserInputType.MouseButton1 and isEquipped then 
		print("MouseButton1 pressed")

		local Anim = Instance.new("Animation")
		Anim.AnimationId = "rbxassetid://16700037866"

		local track
		local success, error = pcall(function()
			track = script.Parent.Parent.Humanoid:LoadAnimation(Anim)
		end)

		if success then
			track.Priority = Enum.AnimationPriority.Action4
			track.Looped = false
			track:Play()
			print("Animation played successfully")
		else
			print("Error loading animation:", error)
		end
	end
end)

I tried this, it still doesnt print anything, the isEquipped variable is definitely set as True and im pressing M1

For me it works.

I tried placing it in starterCharacterScripts but that still doesnt output, maybe it has to do with the place im running it in?

Are you sure you edit the right file? Can you make the rifle print hello?

If you change your code for:

local userInputService = game:GetService("UserInputService")

userInputService.InputBegan:Connect(function(input)
	print('Hello')
end)


You get the “Hello” right?

It wont print hello, what file does it need to be? i have it as a localscript as a child of the tool

Yes. Folder is StarterCharacterScripts , create a Tool , create a localscript inside the tool. Try to create another tool.

Okay, creating a new tool lets it print “hello” not sure why it doesnt work with the rifle though, im using the exact same code (the rifle prints hello, but only when there is no other code aside from the printing)

Try to delete the rifle and rename the new tool.

So, this:

local userInputService = game:GetService("UserInputService")
local plr = game.Players.LocalPlayer
local character = plr.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local isEquipped = true
local crouching = false

userInputService.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.LeftControl and isEquipped == true then
		local Anim = Instance.new("Animation")
		Anim.AnimationId = "rbxassetid://16706440839"
		local track
		track = script.Parent.Parent.Humanoid:LoadAnimation(Anim)
		track.Priority = Enum.AnimationPriority.Action4
		track.Looped = false
		if crouching == true then
			crouching = false
			humanoid.WalkSpeed = 16
			track:Stop()
		else
			humanoid.WalkSpeed = 4
			crouching = true
			track:Play()
			task.wait(track.Length * 0.98)
			track:AdjustSpeed(0)
		end
	end
end)

Doesnt work (does nothing), but this:

local userInputService = game:GetService("UserInputService")

userInputService.InputBegan:Connect(function(input)
	print('Hello')
end)

Outputs “hello”

Both codes are missing the
local userInputService = game:GetService(“UserInputService”)

Oh sorry, i formatted the code block wrong, they both have that statement