Why wont this script work

Hi im trying to make a script for a tool that plays when on pc left click is clicked and on mobile when you tap

local tool = script.Parent
local animationId = "14545918960"  -- Replace with the correct animation ID

local userMouse = game.Players.LocalPlayer:GetMouse()
local animationPlayed = false
local animationTrack = nil

local function playAnimation()
	if animationPlayed then
		return
	end

	animationPlayed = true

	local character = game.Players.LocalPlayer.Character
	local humanoid = character:WaitForChild("Humanoid")

	animationTrack = humanoid:LoadAnimation(animationId)
	animationTrack:Play()
end

userMouse.Button1Down:Connect(function()
	local character = game.Players.LocalPlayer.Character
	local toolEquipped = character:FindFirstChild(tool.Name)

	if toolEquipped then
		playAnimation()
	end
end)

1 Like

What doesn’t work about it?

it does not play the animation when i click and or tap

It’s because you need to create an Animation object, and then :LoadAnimation that object.

Code:

--//Services
local UserInputService = game:GetService("UserInputService")

--//Variables
local tool = script.Parent
local animation = Instance.new("Animation")
animation.AnimationId = "rbxassetid://14545918960"

--//Controls
local animationPlayed = false

--//Functions
local function playAnimation()
	if animationPlayed then
		return
	end

	animationPlayed = true

	local character = game.Players.LocalPlayer.Character
	local humanoid = character:WaitForChild("Humanoid")

	local animationTrack = humanoid:WaitForChild("Animator"):LoadAnimation(animation)
	animationTrack:Play()
end

UserInputService.InputBegan:Connect(function(input, gameProcessedEvent)
	if gameProcessedEvent or input.UserInputState ~= Enum.UserInputState.Begin then
		return
	end
		
	if input.UserInputType == Enum.UserInputType.Touch or input.UserInputType == Enum.UserInputType.MouseButton1 then
		playAnimation()
	end
end)

Keep in mind, your current system only allows you to play the animation once, if you want it to be usable multiple times, then ask me.

i do want it to be usable multiple times and i have an animaton object

basically i want same animation and script as classic sword but with my own model