Variable doesn't do how i want it to

  1. What do you want to achieve? i set up some variables for mob and tower

  2. 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

  3. 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
1 Like

What line is the error in the Output window referring to? The error report will tell you the script name and line number.

It looks like object may not be referring to what you think it does. You can do this to tell you what your script is actually reading as object:

local function setAnimation(object, animName)
    print("object.Name = ", object.Name)
    -- rest of your code

Take a screenshot of the Explorer window so we can see exactly where Upgrades is actually located in the tower in your Workspace.

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)

Captura de pantalla 2024-05-21 175040

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