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)