Best way to load Animation Tracks

Ok so recently Ive been working on a game and I wanna make players be able to drop weapons (Tools) but theres an issue, You see animation tracks need to be loaded into the tool owner but once it is dropped it has a new owner and we have to load them again, Since theres no propper way to know who is the current tool owner I have decided to set a table with nil variables and set them to the animation tracks I will load into the tool owner when a tool is equipped, The issue is that from what I read this could actually be an issue since If I keep loading the animation tracks everytime the tools is equipped it could make more animation tracks.

So if any of you all have a better method for loading animation tracks or knowing who owns the tool it would help me a lot.

Fetching the owner of a tool is actually fairly trivial.

local Players = game:GetService("Players")
local Tool = script.Parent

Tool.Equipped:Connect(function()
	local Character = Tool.Parent
	local Player = Players:GetPlayerFromCharacter(Character)
	print(Player.Name, Player.UserId.." equipped.")
end)

Tool.Unequipped:Connect(function()
	local Player = Tool:FindFirstAncestorOfClass("Player")
	local Character = Player.Character
	print(Player.Name, Player.UserId.." unequipped.")
end)

The events aren’t even required, you can load the animations from the get-go.

--SERVER SCRIPT

local Tool = script.Parent
local Player = Tool:FindFirstAncestorOfClass("Player")
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")
local Animator = Humanoid:WaitForChild("Animator")
local Animation = script.Animation
local AnimationTrack = Animator:LoadAnimation(Animation)
--LOCAL SCRIPT

local Players = game:GetService("Players")
local Player = Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")
local Animator = Humanoid:WaitForChild("Animator")
local Animation = script:WaitForChild("Animation")
local AnimationTrack = Animator:LoadAnimation(Animation)

Here are the two ways in which you can create an AnimationTrack instance from both the server & the client (without needing to recreate the same AnimationTrack instance repetitively).

The issue here is that while your script works, If the owner of the tool decides to drop the tool, Then the animation tracks will play on the previous owner since they are loaded there, Thats why I load them everytime it’s equipped but I dont think my solution is very efficient

I wasn’t aware that your tools were droppable, in that case your current approach of loading the AnimationTrack instance for each new owner is the correct one.

1 Like

Well Ill take your statement as solution then.

local Players = game:GetService("Players")
local Tool = script.Parent
local Animation = script.Animation
local AnimationTrack = nil

Tool.Equipped:Connect(function()
	local Character = Tool.Parent
	local Player = Players:GetPlayerFromCharacter(Character)
	local Humanoid = Character:WaitForChild("Humanoid")
	local Animator = Humanoid:WaitForChild("Animator")
	if not AnimationTrack then
		AnimationTrack = Animator:LoadAnimation(Animation)
	end
	print(Player.Name, Player.UserId.." equipped.")
end)

Tool.Unequipped:Connect(function()
	if AnimationTrack then
		AnimationTrack:Stop()
	end
end)

Tool.AncestryChanged:Connect(function(_, Ancestor)
	if Ancestor == workspace then --Tool dropped.
		AnimationTrack = nil
	end
end)

You could do it like this to avoid reloading the same “AnimationTrack” instance from the same “Animator” instance.

1 Like