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)
Ensure that you have the following line of code to connect the userInputService:
local userInputService = game:GetService("UserInputService")
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.
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")
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.
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)
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)
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)
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)