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.
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.
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)
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.
--// 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.
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