Animation not working correctly?!

Priority: Idle
Looped: true
If that doesnt work idk what to do… I could lend some scripts if u need

1 Like

These images are when its looped and when the priority is Idle

Is the animation published at your own game or at the other game?

Because it will only work if the game itself owns the animation.

I made the animation in the same game and I uploaded it in that game aswell.

Have you put track:Stop() when you unequip it?

This is my current script:

local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local animation = character:WaitForChild("SwordAnimations").swordIdle
local humanoid = character:WaitForChild("Humanoid")
local tool = script.Parent



tool.Equipped:Connect(function()
	local track = humanoid:FindFirstChildOfClass("Animator"):LoadAnimation(animation)
	track:Play()
	print("animation being played")	
	
end)

tool.Unequipped:Connect(function()
	local track = humanoid:FindFirstChildOfClass("Animator"):LoadAnimation(animation)
	track:Stop()
	print("animation being played")	
end)

You’re declaring track again, it’s not the same track. You would like to load the animation once.
I found OOP really useful when programming a tool so go ahead. I would also use Maid/Janitor (Garbage collectors) in order to make this script way better!

--MODULE SCRIPT
local Players = game:GetService("Players")
local player = Players.LocalPlayer

local Tool = {}
Tool.ClassName = "Tool"
Tool.__index = Tool

-- Pass the tool you want to use
function Tool.new(obj: Tool)
	assert(typeof(obj) == "Instance", "Bad tool")
	local self = setmetatable({}, Tool)
	self._tool = obj

	self._character = player.Character or player.CharacterAdded:Wait()
	if self._character then
		self._swordAnims = self._character.SwordAnimtions
		self._swordIdle = self._swordAnims.swordIdle

		local humanoid = self._character:FindFirstChildOfClass("Humanoid")
		local animator = humanoid:FindFirstChild("Animator")

		self._track = animator:LoadAnimation(self._swordIdle)
	end

	self:startListening()

	return self
end

function Tool:startListening()
	local function onEquipped()
		-- Do more stuff if you want
		self._track:Play()
	end

	local function onUnequipped()
		-- Do more stuff if you want
		self._track:Stop()
	end

	self._tool.Equipped:Connect(onEquipped)
	self._tool.Unequipped:Connect(onUnequipped)
end

return Tool
-- LOCALSCRIPT
require(script.Tool).new(script.Parent)

where to put the module? and the localscript? local script inside the tool?

Yes! Make the localscript inside tool and the module inside localscript. The script is untested so let me know if there are any errors

I dont understand what youre saying… Do you mean the module inside the localscript or both inside the Sword (this is the tool object)

This is what im getting if I put it as it shows in the explorer:

Show the localscript, did you require the module?
The localscript should look like this

local Tool = require(script.Tool)
-- We pass the tool (obj)
Tool.new(script.Parent)

This is the localscript inside the tool:

local Tool = require(script.Tool)
Tool.new(script.Parent)

And this is the module script:

--MODULE SCRIPT
local Players = game:GetService("Players")
local player = Players.LocalPlayer

local Tool = {}
Tool.ClassName = "Tool"
Tool.__index = Tool

-- Pass the tool you want to use
function Tool.new(obj: Tool)
	assert(typeof(obj) == "Instance", "Bad tool")
	local self = setmetatable({}, Tool)
	self._tool = obj

	self._character = player.Character or player.CharacterAdded:Wait()
	if self._character then
		self._swordAnims = self._character.SwordAnimtions
		self._swordIdle = self._swordAnims.swordIdle

		local humanoid = self._character:FindFirstChildOfClass("Humanoid")
		local animator = humanoid:FindFirstChild("Animator")

		self._track = animator:LoadAnimation(self._swordIdle)
	end

	self:startListening()

	return self
end

function Tool:startListening()
	local function onEquipped()
		-- Do more stuff if you want
		self._track:Play()
	end

	local function onUnequipped()
		-- Do more stuff if you want
		self._track:Stop()
	end

	self._tool.Equipped:Connect(onEquipped)
	self._tool.Unequipped:Connect(onUnequipped)
end

return Tool

And this is the explorer:

image

Would you be comfortable sending the rbxm file so I can see what’s deeply going on?

Noob Warriors.rbxl (145.2 KB)

Alright it was an easy fix, The moduleScript didn’t replicate to the client yet so it errored right after the player loaded. Here’s the fixes;

For local script:

local Tool = require(script:WaitForChild("Tool"))
Tool.new(script.Parent)

For module scripts (Tool) :

Line 17, just replace line 17 with this 
self._swordAnims = self._character.SwordAnimations

And it works

1 Like

It doesn’t work as I want it to work cause now the full animation is not getting played again

Here:

image

That must be problem with the animation you created, the code works fine so you’ll need to find out what’s the problem with the animation itself. Either it’s keyframes problem or just report it to Bug support team and they eventually will resolve the problem, (find the culprit)

I dont understand anything from what is happening in the module script so if you can explain that that would be great and also when I placed the loadanimation function within the equip function it solved the problem.

but then every time i equiped it it only played a bit of the animation even though i had 1 keyframe. So if you can change the module script or tell me what to change in the module script so the loadanimation function is inside the equip function then maybe that helps

Old script
The main problem you’re loading the animation each time the tool is being equipped, you shouldn’t really do that because it will be the same as loading it once and it less performance friendly. Because each load can maybe be delayed, yielded and it will result that the animation will be only played after the animation has been loaded.

Same you’re doing for the Unequipped signal, you’re loading the track and doing nothing with it. I don’t understand the logic of it.

Module script

We’re using object oriented programming, basically in your scenario is makes tool scripting way easier because you can access the track from everywhere and load the animation first. And it will make the animation playing process better than each-time loading it over and over

  1. We get the character from the player

  2. Check if it’s not nil otherwise it might error out

  3. We get the humanoid and the animator and loading the swordIdle animation on the Animator. Basically loading the animation on the character.

  4. We calling the function :startListening() Basically this function initializes the listeners. The . Equipped listener and .Unequipped listener. Every time the .Equipped/Unequipped is being fired (It’s basically a signal) we call a function that plays the track. In the future if you want to add like maybe datastores and etc you’ll find out doing what I did comes useful and makes the code more readable.


Personally I would add garbage collectors (gc are reclaiming memory which was allocated by the program) in order to do less memory management job and of course have a better performance

You can just google the terms I’ve used such as signal, gc, etc and learn more!