Issues - how can i play an animation when tool is activated?

Hello, I would like to play an animation only when the tool is activated. Could someone give me a hand? I’ve tried to do this, but it throws an error:

image

-- Get necessary services and objects
local replicatedStorage = game:GetService("ReplicatedStorage")
local players = game:GetService("Players")

local remoteEventRemoveGuitar = replicatedStorage:WaitForChild("remoteEventRemoveGuitar")
local remoteEventAddGuitar = replicatedStorage:WaitForChild("remoteEventAddGuitar")

-- Event handler for removing the tool
remoteEventRemoveGuitar.OnServerEvent:Connect(function(player)
	print("Server received request to remove tool")

	-- Search for the tool in the player's backpack
	local toolInBackpack = player.Backpack:FindFirstChild("ElectricGuitar")
	if toolInBackpack then
		toolInBackpack:Destroy() -- Remove the tool if found in the backpack
		print("Tool removed from player's Backpack:", player.Name)
	else
		print("Player does not have the tool in their Backpack")
	end

	-- Search for the tool equipped on the player's character
	local toolEquipped = player.Character and player.Character:FindFirstChild("ElectricGuitar")
	if toolEquipped then
		toolEquipped:Destroy() -- Remove the tool if it is equipped
		print("Tool removed from player's Character:", player.Name)
	end
end)

-- Event handler for adding the tool
remoteEventAddGuitar.OnServerEvent:Connect(function(player)
	print("Server received request to add tool")

	-- Check if the tool is already in the backpack or equipped
	local toolInBackpack = player.Backpack:FindFirstChild("ElectricGuitar")
	local toolEquipped = player.Character and player.Character:FindFirstChild("ElectricGuitar")

	if not toolInBackpack and not toolEquipped then
		-- Clone and add the tool to the player's backpack
		local tool = game:GetService("ReplicatedStorage").ElectricGuitar:Clone()
		tool.Parent = player.Backpack
		print("Tool added to player's Backpack:", player.Name)

		-- Play animation when the tool is added
		local humanoid = player.Character and player.Character:FindFirstChildOfClass("Humanoid")
		if humanoid then
			local animation = humanoid:LoadAnimation(replicatedStorage:WaitForChild("Initial pose"))
			animation:Play()
		end
	else
		print("Player already has the tool in their Backpack or equipped")
	end
end)

when you :LoadAnimation() you need an “Animation” instance.

example:

animationInstance = Instance.new("Animation")
humanoid = Character.Humanoid

animation = humanoid:LoadAnimation(animationInstance)
animation:Play()
1 Like

Oh I understand, so it should be like this, but how could I make this happen only when the tool is activated and not before? :pray:

-- Play animation when the tool is added
		local humanoid = player.Character and player.Character:FindFirstChildOfClass("Humanoid")
		if humanoid then
			local animationInstance = Instance.new("Animation")
			animationInstance.AnimationId = "rbxassetid://15735655906" -- Replace with your actual Animation Id

			local animation = humanoid:LoadAnimation(animationInstance)
			animation:Play()
		end
equipped = false

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

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

if equipped == true and --[[whatever your other statements are]] then
animation:Play()
end)

heres a little prototype. if i understand correctly you want it to play when example you click and the tool is equipped right? unless if you want the animation to play as soon as you equip the tool:

tool.Equipped:Connect(function()
animation:Play()
end)

tool.Unequipped:Connect(function()
animation:Stop()
end)

I tried to play the animation when the tool is activated and deactivated but it is not working for me :frowning:

-- Play animation when the tool is added
		local humanoid = player.Character and player.Character:FindFirstChildOfClass("Humanoid")
		if humanoid then
			local animationInstance = Instance.new("Animation")
			animationInstance.AnimationId = "rbxassetid://15735715919" -- Replace with your actual Animation Id

			local animation = humanoid:LoadAnimation(animationInstance)

			-- Check if the tool is activated
			tool.Activated:Connect(function()
				animation:Play()
				print("playing animation")
			end)

			-- Stop animation if the tool is deactivated
			tool.Deactivated:Connect(function()
				animation:Stop()
				print("animation stopped")
			end)
		end

Maybe try this instead. I am not familiar with how and works in variables, so correct me if what I am doing is pointless.

local char = player.Character or Player.CharacterAdded:Wait()
local humanoid = char:FindFirstChild("Humanoid")

The animation it has to play isn’t the one that is in the “AnimSaves” model when you animate something, when you go in the animation editor and click on the three dots, you can actually click on “Publish to Roblox” in order to get the animation id to insert in an Animation instance, which is what you have to play


image
image

One tip I should give is, since you’re doing an animation for a tool, is to click on the 3 dots again in the animation editor, and set the animation priority to “Action”
image

Also, to play an animation it’s very easy, here’s a quick way to do it:

What the local script could look like (it's simple, really)
script.Parent.Equipped:Connect(function()
	script.Parent.EquipRemote:FireServer(true) -- Fires true, since the player has it equipped
end)

script.Parent.Unequipped:Connect(function()
	script.Parent.EquipRemote:FireServer(false) -- Fires false, since the player no longer has it equipped
end)
Server script (also not that hard)
function playAnim(player,anim)
	local character = player.Character
	if character then
		local humanoid = character:FindFirstChildOfClass("Humanoid")
		if humanoid then
			humanoid:LoadAnimation(anim):Play()
		end
	end
end


script.Parent.EquipRemote.OnServerEvent:Connect(function(player,statement)
	if statement == true then
		playAnim(player,script.Parent.EquipAnim) -- The animation is ALWAYS an instance and NEVER an animation Id
	else
		playAnim(player,script.Parent.UnequipAnim) -- The animation is ALWAYS an instance and NEVER an animation Id
	end
end)

image

Hopefully this helps! (also don’t delete the animation that’s in the tool, put it in an “AnimSaves” model inside a rig)

1 Like