Animations Not Working

Hey there!

I am currently making a game, and it has tools that require animations. But the problem is that the animations aren’t loading/working. These tools are saved in a datastore so when the player leaves, all their tools will be in the inventory but the animations dont load or work, the animations work fine when i give them to myself using the dev console just not after being saved with the datastore.

There are no output errors.

Here is the animation script:

local Player = game.Players.LocalPlayer
local character = Player.Character
local Animation = script.Parent:FindFirstChild("Idle")
local SwingAnimation = script.Parent:FindFirstChild("SwingAnimation")
local Equipped = false
local canSwing = true
repeat wait() until character
local CharacterAnim = character.Humanoid:LoadAnimation(Animation)
local CharacterSwingAnim = character.Humanoid:LoadAnimation(SwingAnimation)

script.Parent.Equipped:Connect(function()
	if Equipped == false then
		CharacterAnim:Play()
		Equipped = true
	end
end)

script.Parent.Unequipped:Connect(function()
	if Equipped == true and CharacterAnim.IsPlaying == true then
		CharacterAnim:Stop()
		Equipped = false
	end
end)

script.Parent.Activated:Connect(function()
	if canSwing == true then
		CharacterSwingAnim:Play()
		character.Humanoid.WalkSpeed = 0
		canSwing = false
		wait(2.1)
		canSwing = true
		character.Humanoid.WalkSpeed = 16
		end
	end)

The Datastore script:

-- This script works fine, it properly saves the current tools in the Players backpack and Starter Gear

local DataStoreService = game:GetService("DataStoreService")
local savedTools = DataStoreService:GetDataStore("ToolData")

local toolsFolder = game.ServerStorage.Tools

game.Players.PlayerAdded:Connect(function(plr)
	local toolsSaved = savedTools:GetAsync(plr.UserId.."-tools") or {}
	for i, toolSaved in pairs(toolsSaved) do
		if toolsFolder:FindFirstChild(toolSaved) then
			toolsFolder[toolSaved]:Clone().Parent = plr.Backpack
			toolsFolder[toolSaved]:Clone().Parent = plr.StarterGear
		end
	end
	plr.CharacterRemoving:Connect(function(char)
		char.Humanoid:UnequipTools()
	end)
end)

game.Players.PlayerRemoving:Connect(function(plr)
	local ownedTools = {}
	for i, toolInBackpack in pairs(plr.Backpack:GetChildren()) do
		table.insert(ownedTools, toolInBackpack.Name)
	end
	local success, errormessage = pcall(function()
		savedTools:SetAsync(plr.UserId.."-tools", ownedTools)
	end)
	if errormessage then warn(errormessage) end
end)

The datastore script was taken from GamerM8’s simulator tutorial.

Thanks for the help! :slight_smile:

I don’t know where the error could be but if you added a print("Successfully played animation") script after the part where the script plays the animation, you could figure out where the problem is and how to fix it.

For example, you can put it after:

Also, is the animation script a local script or server script and where is it placed?

The script is a local script and is located within the tool.

Edit: The print function doesnt seem to be printing anything.

So I suggest for you to make the local script fire a RemoteEvent to a server script. The server script can be placed inside of the Workspace. This will help make the animation play for the server and not locally.


Steps:

  1. Make a RemoteEvent for the local script to fire to the server script (placed in workspace).
game.ReplicatedStorage.<EVENT>:FireServer("ANIAMTIONID")
  1. In the server script, put in the following code:
game.ReplicatedStorage.<EVENT>.OnServerEvent:Connect(function(player, animationID)
	local animation = Instance.new("Animation")
	animation.AnimationId = "https://www.roblox.com/Asset?ID="..animationID
	
	local loadedAnimation = game.Workspace[player.Name].Humanoid:LoadAnimation(animation)
	loadedAnimation:Play()
	wait(-- How long?)
	loadedAnimation:Stop()
end)
  1. You’re done! Test it out and check for any errors!

Change the code to match your game or it will not work

1 Like