Idle Animation doesn't Load on Spawn?

I’m currently working on a custom inventory system for a game of mine and a friend. For some reason, whenever the character loads (for the first time and on respawn), the real character’s animation doesn’t play, yet the ViewModel’s animation plays just fine.

ViewModel footage: https://gyazo.com/dbdc7f2a0f5d3b295b2a45bd23dda673

Images

When spawning with the item already in hand:

After reselecting the item:

Relevant Code

In short, what it does is stop the animations of the previous item, and play the new item’s animations. It’s supposed to work theoretically…

EquippedItem:GetValChanged(function(ItemValue)
	EffectsModule:ParentItemModels(ItemValue,PreviousItemValue,ItemsFolder,EquippedItem)
	
	if PreviousItemValue then
		local PreviousItemModuleTable = ItemModuleTables[PreviousItemValue]
		if PreviousItemModuleTable then
			for _,AnimationTable in pairs(PreviousItemModuleTable.Animations.All) do
				for _,Animation in pairs(AnimationTable) do
					Animation:Stop(0)
				end
			end
			
			if not table.find(Items,PreviousItemValue) then
				ItemModuleTables[PreviousItemValue] = nil
			end
		end
	end
	PreviousItemValue = ItemValue

	if ItemValue then
		local ItemModule = ItemValue.ItemModule.Value
		if ItemModule then
			local ItemModuleTable = ItemModuleTables[ItemValue]
			
			if not ItemModuleTable then
				ItemModuleTable = {}
				
				
				local Animations = {
					All = {}
				}
				
				ItemModuleTable.Animations = Animations
				
				
				local ActionsFolder = ItemModule:FindFirstChild("Actions")
				if ActionsFolder then
					local IdleAnimation = ActionsFolder:FindFirstChild("Idle")
					if IdleAnimation then
						Animations.All.Idle = {Animator:LoadAnimation(IdleAnimation),VM_Animator:LoadAnimation(IdleAnimation)}
					end
				end
				
				
				ItemModuleTables[ItemValue] = ItemModuleTable
			end
			
			local IdleAnimationTable = ItemModuleTable.Animations.All.Idle
			if IdleAnimationTable then
				for _,Animation in pairs(IdleAnimationTable) do
					Animation:Play(0)
				end
			end
		end
	end
end)

Although using RunService.PreRender:Wait() fixes the problem, it also creates a new problem, which is breaking the item model display when you select 2 items at once. It also plays 2 idle animations.
… Scratch that, it still happens.

More Images

Normal:

Not normal:

I genuinely have no idea what’s wrong here…
Thanks for reading :slight_smile:

Maybe try checking if the player has joined? Try This:

game.Players.PlayerAdded:Connect(function(player)
	EquippedItem:GetValChanged(function(ItemValue)
		EffectsModule:ParentItemModels(ItemValue,PreviousItemValue,ItemsFolder,EquippedItem)

		if PreviousItemValue then
			local PreviousItemModuleTable = ItemModuleTables[PreviousItemValue]
			if PreviousItemModuleTable then
				for _,AnimationTable in pairs(PreviousItemModuleTable.Animations.All) do
					for _,Animation in pairs(AnimationTable) do
						Animation:Stop(0)
					end
				end

				if not table.find(Items,PreviousItemValue) then
					ItemModuleTables[PreviousItemValue] = nil
				end
			end
		end
		PreviousItemValue = ItemValue

		if ItemValue then
			local ItemModule = ItemValue.ItemModule.Value
			if ItemModule then
				local ItemModuleTable = ItemModuleTables[ItemValue]

				if not ItemModuleTable then
					ItemModuleTable = {}


					local Animations = {
						All = {}
					}

					ItemModuleTable.Animations = Animations


					local ActionsFolder = ItemModule:FindFirstChild("Actions")
					if ActionsFolder then
						local IdleAnimation = ActionsFolder:FindFirstChild("Idle")
						if IdleAnimation then
							Animations.All.Idle = {Animator:LoadAnimation(IdleAnimation),VM_Animator:LoadAnimation(IdleAnimation)}
						end
					end


					ItemModuleTables[ItemValue] = ItemModuleTable
				end

				local IdleAnimationTable = ItemModuleTable.Animations.All.Idle
				if IdleAnimationTable then
					for _,Animation in pairs(IdleAnimationTable) do
						Animation:Play(0)
					end
				end
			end
		end
	end)
end) 

1 Like

My script is inside StarterCharacterScripts, so detecting when a player joins won’t be effective. Also, the tools that I use are not Roblox’s regular tools, they’re ObjectValues with their value property set to their models so it’ll be easier for scripts to access their model.

Have you also tried putting so it would check if the character has loaded?

1 Like
--// Variables //--

--// Player
local Player		=	Players.LocalPlayer
local PlayerScripts	=	Player:WaitForChild("PlayerScripts")
local Character		=	Player.Character
local Humanoid		=	Character:WaitForChild("Humanoid")
local Animator		=	Humanoid:WaitForChild("Animator")


--// ViewModel
local ViewModelValue = PlayerScripts:WaitForChild("ViewModel")
ViewModelValue:GetPropertyChangedSignal("Value"):Wait()
local ViewModel = ViewModelValue.Value

local VM_Animator = ViewModel:WaitForChild("Humanoid"):WaitForChild("Animator")

It uses the Animator of the real Humanoid and the Animator of the ViewModel humanoid. And since the script is inside the character to begin with, then it doesn’t need to check if the character has loaded.
I’m starting to think that this is more of a bug, or maybe I need to place my code better.

I’ve also had same problem on my previous tools but i never used viewmodels

1 Like

Alright, so I added a yield for 3 seconds, and apparently the .IsPlaying property is set to false later??

local IdleAnimationTable = ItemModuleTable.Animations.All.Idle
if IdleAnimationTable then
	for _,Animation in pairs(IdleAnimationTable) do
		Animation:Play(0)
		task.spawn(function()
			task.wait(3)
			print(Animation.IsPlaying) -- Prints out "false" for the real character's animation.
		end)
	end
end


that’s the default animate script man

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