Animation not working correctly?!

I made the sword in blender so it isnt a stock sword and it does not have any old scripts. I tried setting the animation priority to action and the other actions as well before but that didn’t solve it as well.

2 Likes

is it playing at all? do you have a debug print on equip just to make sure the functions working?

2 Likes

I put the line where it loads the animatin in the equip function which it did solve but now the full animation isn’t played but only 1 keyframe every time i equip and unequip the tool so ima try aand change the keyframes to only 1 and see if that works.

2 Likes

I deleted all the keyframes except 1 but that didn’t solve it here are images(recorder doesnt want to film…)

image

image

image

2 Likes

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