What do you want to achieve? i set up some variables for mob and tower
What is the issue? i get a error saying Upgrades is not a valid member of Model "Workspace.Mobs.Zombie" which is confusing because upgrades should only be accessed through tower and not mob
What solutions have you tried so far? None
local function setAnimation(object, animName)
local humanoid = object:WaitForChild("Humanoid")
local MobAnimationsFolder = object:WaitForChild("Animations")
local TowerAnimationsFolder = object.Upgrades[object.Level.Value]:WaitForChild("Animations")
if humanoid and MobAnimationsFolder and TowerAnimationsFolder then
local TowerAnimationObject = TowerAnimationsFolder:WaitForChild(animName)
local MobAnimationObject = MobAnimationsFolder:WaitForChild(animName)
if TowerAnimationObject and MobAnimationObject then
local animator = humanoid:FindFirstChild("Animator") or Instance.new("Animator", humanoid)
local playingTracks = animator:GetPlayingAnimationTracks()
for i, track in ipairs(playingTracks) do
if track.Name == animName then
return track
end
end
local AnimationTrack = animator:LoadAnimation(TowerAnimationObject, MobAnimationObject)
return AnimationTrack
end
end
end
you should add checks to determine whether the object is a mob or a tower and then access the appropriate folders accordingly.
Here’s it is
local function setAnimation(object, animName)
local humanoid = object:WaitForChild("Humanoid")
local MobAnimationsFolder = object:FindFirstChild("Animations")
local TowerAnimationsFolder = nil
-- Check if the object is a tower and has an Upgrades folder
if object:FindFirstChild("Upgrades") and object:FindFirstChild("Level") then
TowerAnimationsFolder = object.Upgrades[object.Level.Value]:FindFirstChild("Animations")
end
if humanoid and MobAnimationsFolder then
-- Check if the animation exists in the MobAnimationsFolder
local MobAnimationObject = MobAnimationsFolder:FindFirstChild(animName)
if TowerAnimationsFolder then
-- Check if the animation exists in the TowerAnimationsFolder
local TowerAnimationObject = TowerAnimationsFolder:FindFirstChild(animName)
if TowerAnimationObject and MobAnimationObject then
local animator = humanoid:FindFirstChild("Animator") or Instance.new("Animator", humanoid)
local playingTracks = animator:GetPlayingAnimationTracks()
for i, track in ipairs(playingTracks) do
if track.Name == animName then
return track
end
end
local AnimationTrack = animator:LoadAnimation(TowerAnimationObject, MobAnimationObject)
return AnimationTrack
end
elseif MobAnimationObject then
-- If it's a mob and only MobAnimationObject exists
local animator = humanoid:FindFirstChild("Animator") or Instance.new("Animator", humanoid)
local playingTracks = animator:GetPlayingAnimationTracks()
for i, track in ipairs(playingTracks) do
if track.Name == animName then
return track
end
end
local AnimationTrack = animator:LoadAnimation(MobAnimationObject)
return AnimationTrack
end
end
end
you mean this? sorry if i didnt understand by the way when i click the error it says on this local TowerAnimationsFolder = object.Upgrades[object.Level.Value]:WaitForChild("Animations")
(DON’T WORRY IT’S FIXED BUT THANKS FOR TRYING TO HELP)