Hello, the following script is supposed to play an animation for an item drop upon player action (indicated by function ChestPools:spawnItem). The animation also has several keyframes spanning 0.6 sec. However, the animation length is returning a length of 0 and is not playing (even without adjusting the speed to 0).
Also optimization is not important, as I am more concerned with solving the current problem! Thanks!
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ChestPools = {}
local Items = {}
Items["Common"] = {"Apple"}
Items["Rare"] = {"GreenApple"}
Items["Legendary"] = {"DecayedApple"}
-- Load and store animations
local function loadAnimation(item)
local itemDrop = Instance.new("Animation")
itemDrop.AnimationId = "rbxassetid://128752945782630"
local animator = Instance.new("Animator")
animator.Parent = item:WaitForChild("Humanoid")
local track = animator:LoadAnimation(itemDrop)
track.Name = "itemDrop"
return track
end
function getItems(rarities)
local items = {}
for _, rarity in rarities do
items[rarity] = {}
for _, item in Items[rarity] do
table.insert(items[rarity], item)
end
end
return items
end
ChestPools["Normal"] = {
Chances = {
Common = 70.0,
Rare = 20.0,
Legendary = 10.0
},
Pool = getItems({"Common", "Rare", "Legendary"})
}
ChestPools["Big"] = {}
ChestPools["Legendary"] = {}
function ChestPools:RandomPull(chest)
--Pull a random rarity and an item ascociated with the rarity
local rarity = nil
local random = math.random(1, 100)
local counter = 0
local chestType = chest:GetAttribute("chestType")
for rarityKey, rarityChance in ChestPools[chestType].Chances do
counter = counter + rarityChance
if random <= counter then
rarity = rarityKey
break
end
end
local pool = ChestPools[chestType].Pool[rarity]
local item = pool[math.random(1, #pool)]
local spawnPos = chest.PrimaryPart.Position + Vector3.new(0, 0, 0)
local item = ReplicatedStorage.Items[rarity][item]:Clone()
item:SetPrimaryPartCFrame(CFrame.new(spawnPos) * CFrame.Angles(0, 0, 0))
local tracks = loadAnimation(item)
return {item, tracks}
end
function ChestPools:spawnItem(chest, itemInfo)
local item = itemInfo[1]
local itemRarity = item:GetAttribute("Rarity")
local animationTrack = itemInfo[2]
task.wait(0.5)
item.Parent = workspace.Items[itemRarity]
print("Loaded animation track:", animationTrack)
animationTrack:Play()
print("Started playing animation track")
print(animationTrack.Length)
task.wait(animationTrack.Length/2)
print(animationTrack)
while workspace:Raycast(item.RaycastPart.Position, -item.RaycastPart.CFrame.UpVector * ((item.RaycastPart.Size.Y/2)+0.1)) == nil do
task.wait()
end
animationTrack:AdjustSpeed(0)
print("Animation track finished playing")
end
return ChestPools
EDIT:
Each chest has a script that calls RandomPull (also calling loadAnimation). This should pre-load the animation as soon as the chest is created and the animation will wait to play until a player interaction. All of the items use the same animation and a new animation instance is created for each chest/item. I have tested this animation before and it worked perfectly fine (animation length and all). It just does not want to work with the random factor of the items for some reason and I cant figure out why.