Need help with a custom animation script

I am working on a custom animation script for my npc that allows for animations to ‘toggle’ on when needed (while allowing for multiple to run at once). However, the Jump/Fall only seems to work once.

local Anim_List = {}
local Dist = 0
local Speed = 0
Cur_Spd = 0
local pose = ""
local Old_Pose = ""
local Whitelist = {}
local Loaded_Tracks = {Tracks = {},Inst = {},Conn = {}}
local Figure = script.Parent
local Hum = Figure:FindFirstChildOfClass("Humanoid")
local Animator = Hum:FindFirstChildOfClass("Animator")

local Filter = OverlapParams.new()
Filter.FilterDescendantsInstances = {}
Filter.FilterType = Enum.RaycastFilterType.Exclude
for Ind, Val in Figure:GetDescendants() do
	if Val:IsA("BasePart") then
		table.insert(Filter.FilterDescendantsInstances,Val)
	end
end

local StepSound:Sound = Figure.PrimaryPart:FindFirstChild("StepSound")
local Get_Dist = Figure.Script.Get_Dist
local Plr = nil
local CurrentAnim = ""
local CurrentInstance = nil
local Current:AnimationTrack = nil
local currentAnimKeyframeHandler = nil
local currentAnimSpeed = 1
local emoteNames = { ["wave"] = false, ["point"] = false, ["dance1"] = true, ["dance2"] = true, ["dance3"] = true, ["laugh"] = false, ["cheer"] = false, ["attack"] = false}
local jumpAnimTime = 0
local jumpAnimDuration = 0.3
local d = 0

Eq_List = {Eq = {["HighGain"] = -20,["MidGain"] = 0,["LowGain"] = 0}, Pitch = {["Pitch"] = 1}}

Randomize_Eq = function()
	for Inst, Gain in Eq_List.Eq do
		StepSound.Eq[Inst] = Gain - math.random(0,2)
	end
	for Inst, Pitch in Eq_List.Pitch do
		StepSound.P[Inst] = Pitch + math.random(-0.05,0.05)
	end
end

for Num, Ins in script:GetChildren() do
	if #Ins:GetChildren() > 0 then
		Anim_List[Ins.Name] = {Anims = {I = Ins:GetChildren(),W = {}}, Total_Weight = 0,Priority = Ins:GetAttribute("Priority"),Connections = nil}
		Whitelist[Ins.Name] = false
	end
end

for i,v in Anim_List do
	v.Total_Weight = 0
	for N,Ins in v.Anims.I do
		if Ins:GetAttribute("Weight") then
			v.Total_Weight += Ins:GetAttribute("Weight")
			v.Anims.W[N] = Ins:GetAttribute("Weight")
		else
			v.Total_Weight = 10
		end
	end
end
print(Anim_List)

local DB = false

function Anim(I)
	local roll = math.random(1, Anim_List[I].Total_Weight) 
	local origRoll = roll
	local idx = 1
	while (roll > Anim_List[I].Anims.W[idx]) do
		roll = roll - Anim_List[I].Anims.W[idx]
		idx = idx + 1
	end
	local anim = Anim_List[I].Anims.I[idx]
	if Whitelist[I] == true then
	if anim ~= Loaded_Tracks.Inst[I] then
		print(I .. " " .. idx .. " [" .. origRoll .. "]")
		if Loaded_Tracks.Tracks[I] ~= nil or not Whitelist[I] then
			Loaded_Tracks.Tracks[I]:Stop(0.1)
			Loaded_Tracks.Tracks[I]:Destroy()
		end
		
		Loaded_Tracks.Tracks[I] = Animator:LoadAnimation(anim)
		Loaded_Tracks.Tracks[I].Name = I
		Loaded_Tracks.Tracks[I].Priority = Enum.AnimationPriority[Anim_List[I].Priority]
		Loaded_Tracks.Tracks[I]:Play(0.1)
		Loaded_Tracks.Inst[I] = anim
		
		if Loaded_Tracks.Conn[I] ~= nil then
			Loaded_Tracks.Conn[I]:Disconnect()
		end
		
		Loaded_Tracks.Conn[I] = Loaded_Tracks.Tracks[I].KeyframeReached:Connect(function(Key)
			KeyReach(Key,I)
		end)
	end 
	elseif Loaded_Tracks.Tracks[I] ~= nil then
		Loaded_Tracks.Tracks[I]:Stop(0.1)
		Loaded_Tracks.Tracks[I]:Destroy()
		if Loaded_Tracks.Conn[I] ~= nil then
			Loaded_Tracks.Conn[I]:Disconnect()
		end
	end
end

function Stop(I)
	if Loaded_Tracks.Tracks[I] ~= nil then
		Loaded_Tracks.Tracks[I]:Stop(0.1)
		Loaded_Tracks.Tracks[I]:Destroy()
		Loaded_Tracks.Conn[I]:Disconnect()
	end
end

function KeyReach(Key,Track)
	if Key == "End" then
		Anim(Track)
	end
end

function Play_Anims()
	for I,V in Anim_List do
		if Whitelist[I] then
			if Loaded_Tracks.Tracks[I] then
				if Loaded_Tracks.Tracks[I].IsPlaying then
					return
				end
			end
			Anim(I)
		elseif Whitelist[I] == false then
			Stop(I)
		end
	end
end
Run = Hum.Running:Connect(function(spd)
	if spd > 0 then
		pose = "walk"
	else
		pose = "idle"
	end
end)
Jump = Hum.Jumping:Connect(function(Active)
	pose = "jump"
	if Loaded_Tracks.Tracks["jump"] and Active then
		jumpAnimTime = Loaded_Tracks.Tracks["jump"].Length
	end
end)
Fall = Hum.FreeFalling:Connect(function(Active)
	if jumpAnimTime > 0 and Active then
		return
	end
	pose = "fall"
end)
Land = Hum.StateChanged:Connect(function(o,n)
	if n == Enum.HumanoidStateType.Landed or o == Enum.HumanoidStateType.Landed then
		pose = "idle"
	end
end)
HB = game["Run Service"].Heartbeat:Connect(function()
	for I,V in Anim_List do
		if emoteNames[I] == nil then
			if I ~= pose then
				Whitelist[I] = false
				Stop(I)
			else
				Whitelist[I] = true
			end
		end
	end
	if jumpAnimTime > 0 then
		jumpAnimTime -= 1/60
	end
	Play_Anims()
end)
3 Likes

Wrong section. I would change it to Help and Feedback > Scripting Support

can u record a clip of what’s happening? could provide more information to better understand cause I’m not having a good time reading this code

Here’s the clip

1 Like

it’s probably worth to check Active and jumpAnimTime since it doesn’t print the animation playing like before when jumping twice

1 Like

Not just that, it doesn’t play either animation either, it just stops idle

1 Like

do all the other animations work besides the jumping and falling ones? also it makes sense since they’re related anyway

1 Like

I only have the Walk, Idle, and Jump/fall anims implemented.
Besides, what even is ‘Active’ in the context of the Jump and FreeFall connections?

1 Like

have u tried checking whether the whitelist table ever gets updated properly?

1 Like

sorry lol I was a bit dumb with the last post but Active just means it’s entering that state from the looks of it

1 Like

Just did something, and now the animations play… long after they were supposed to.

1 Like

Found the issue! Apparently with the way Upd() works, it wasn’t updating Whitelist properly, leading to missed assignments of variables

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.