Animating a tool

Hi there, I’m something of a beginner at scripting. At the moment, I’m trying to script a sword tool so that it plays an animation when clicked. This animation depicts how someone would dub a knight; that is, raising the sword to rest on one shoulder and then over the head to rest on the other. The sword can be moved from shoulder to shoulder as many times as one wishes as seen in the code below.

The tool includes a script, localscript, and folder of remotes. The localscript will fire a remote when the player clicks, while the script (partially seen below) will listen for this remote and then execute the dub function. All output is as should be but, of the animation, only the character’s head moves where the arm should also be.

local tool = script.Parent
local uis = game:GetService("UserInputService")
local player
	if tool.Parent:IsA("Backpack") then
		player = tool.Parent.Parent
	elseif tool.Parent:IsA("Model") then
		player = game.Players:GetPlayerFromCharacter("tool.Parent")
	end
local char = player.Character
while char.Parent == nil do
	char.AncestryChanged:wait()
end

local dub = tool.Animations:FindFirstChild("Dub")
local dubTrack = char:WaitForChild("Humanoid"):LoadAnimation(dub)

local tool_check = false
local rightShoulder = false
local leftShoulder = false
local raised = false

local function dub()
	if not raised then 
		raised = true
		dubTrack:Play()
	elseif raised and leftShoulder then
		dubTrack:AdjustSpeed(1)
		leftShoulder = false
	elseif raised and rightShoulder then
		dubTrack:AdjustSpeed(-1)
		rightShoulder = false
	end
	
	dubTrack:GetMarkerReachedSignal("lshoulderSet"):Connect(function()
		print("sword set to left shoulder")
		dubTrack:AdjustSpeed(0.0)
		leftShoulder = true
	end)
	
	dubTrack:GetMarkerReachedSignal("rshoulderSet"):Connect(function()
		print("sword set to right shoulder")
		dubTrack:AdjustSpeed(0.0)
		rightShoulder = true
	end)
end

Originally, I had this working. All code was in one localscript and there were no remotes or functions. The issue was that this animation only played locally and was not replicated to anyone else in the server. Obviously, that was a problem and I think that by using remotes it should no longer be (?). I do not know why the animation is not currently working either.

Additionally, this is an R6 animation.

If you are a beginner, you need to start off with the basics. Just make a variable with the sword animation but publishing the animation then calling. Then do a varaible with the WaitForChild:(“Animation”)

You don’t need to use remotes for this. Animations loaded and played by the client through their humanoid should replicate to the server, are you sure it is an issue with the script and not the animation? Put a buttload of print("") statements throughout the different sections of dub() to see precisely where something has gone wrong. As jumbo suggested, it might be an issue where the animation hasn’t loaded in time for the script to :LoadAnimation it onto the humanoid. Try doing this instead:

local dub = tool:WaitForChild("Animations"):WaitForChild("Dub")

You don’t need to use remotes for this.

I did think that this was the case, which is why I originally wrote this in a localscript:

local tool = script.Parent

local player = game:GetService("Players").LocalPlayer
local char = player.Character
while not char or not char.Parent do
	char = player.Character
	wait()
end

local uis = game:GetService("UserInputService")

local dub = tool:WaitForChild("Animations"):WaitForChild("Dub")
local dubTrack = char:WaitForChild("Humanoid"):LoadAnimation(dub)

local tool_check = false
local rightShoulder = false
local leftShoulder = false
local raised = false

tool.Equipped:Connect(function()

	raised = true
	uis.InputBegan:Connect(function(input,gameProcessedEvent)
		if gameProcessedEvent then return end
		if input.UserInputType == Enum.UserInputType.MouseButton1 and not raised then 
			raised = true
			dubTrack:Play()
		if input.UserInputType == Enum.UserInputType.MouseButton1 and raised and leftShoulder then
			dubTrack:AdjustSpeed(1)
			leftShoulder = false
		elseif input.UserInputType == Enum.UserInputType.MouseButton1 and raised and rightShoulder then
			dubTrack:AdjustSpeed(-1)
			rightShoulder = false
		end
	end)
end)


local ActiveTracks = char.Humanoid:GetPlayingAnimationTracks()

tool.Unequipped:Connect(function()
	tool_check = false
	for _,v in pairs(ActiveTracks) do
	    v:Stop()
	end
	print("Dubbing Sword unequipped.")
	dubTrack:Stop()
	wait()
	print("Animation stopped.")
	rightShoulder = false
	leftShoulder = false
	raised = false
end)

dubTrack:GetMarkerReachedSignal("lshoulderSet"):Connect(function()
	print("sword set to left shoulder")
	dubTrack.Priority = Enum.AnimationPriority.Action
	dubTrack:AdjustSpeed(0.0)
	leftShoulder = true
end)

dubTrack:GetMarkerReachedSignal("rshoulderSet"):Connect(function()
	print("sword set to right shoulder")
	dubTrack.Priority = Enum.AnimationPriority.Action
	dubTrack:AdjustSpeed(0.0)
	rightShoulder = true
end)

I tested what you both suggested to no success. The animations still play locally but do not replicate to the server.

I also don’t think this is an issue with loading the animation since the head movement of it is played (and even replicated briefly, before it snaps back…?) but the arm, which is core to the animation, remains static. I just don’t understand what’s going wrong.

  1. Did you set correct animation priority?
  2. Did you uncheck the property ‘RequiresHandle’?

If you are seeing part of the animation, then yeah it could be an animation priority issue. Make sure you set it to “Action”, as to avoid things like idle animations overriding it.