Animation not loading

I’m currently looking to fix this animation script, and I’m not that good with animations. It’s a seat animation, and I’m trying to make the animation load when the player sits down on the seat. I’ve been stuck on this for the past 45 minutes, and no matter what debugging I do, nothing seems to work.

Is there any way I can somehow fix this issue? It’s a script in ServerScriptService.

-- Variables
local LastOccupant

-- Code
for _,v in pairs(workspace:GetDescendants()) do
	if v:IsA("Seat") and v.Name == "LongSeat" then
		v.Changed:Connect(function(Property)
			if Property == "Occupant" then
				if v.Occupant then
					local FoundOccupant = v.Occupant
					local Animation = v.Animation
					wait(0.1)
					LoadedAnimation = FoundOccupant.Parent.Humanoid:LoadAnimation(Animation.AnimationId)
					LoadedAnimation:Play()
					LastOccupant = FoundOccupant
					if not v.Occupant then
						if LastOccupant then
							LoadedAnimation:Stop()
							LoadedAnimation:Remove()
						end
					end
				end
			end
		end)
	end
end```

Did you define the animation itself?
for example:
local anim = Instance.new('Animation')
anim.AnimationId = "www.roblox.com/asset/id?=" --Here You'd put the Anim ID

Yes, the Animation is the child of the seat.

Screenshot descendants please?

Okay so just an update, I was able to figure something out and it partially works. Now, the animation just randomly stops? A GIF is posted below for reference.

https://gyazo.com/2eaa72b592f4b2d924c30e7109ec2bde

image

Are your animations looped? Did you set the animation priority to “Action”?

I believe they are looped, I’ll have to check in on the priority.

1 Like

What you could do is be a little tricky and change the sit animation’s AnimationId from the Roblox animate script.
Here’s how the setup would be:
image
and here’s the script:

---------- VARIABLES ----------
local Seat = script.Parent
local Animation = Seat.SitAnim
---------- CONNECTIONS ----------
Seat:GetPropertyChangedSignal("Occupant"):Connect(function() -- if a property in the seat changes
	local occupant = Seat.Occupant
 
	if occupant then -- if we do have an occupant then do the stuff below
		local character = occupant.Parent --get the character
		character.Animate.sit.SitAnim.AnimationId = Animation.AnimationId --change the animation id
	end
end)
  1. Do you own this animation? If not, then you can’t play it.
  2. Are the Joint M6D the same as used to create this animation? (Same Part names, not M6D names)
  3. And unlikely: are the parts, that are suppose to move, descendant of the parent of the seat? (This was an issue for me once)

EDIT 1: Also try to move LastOccupant variable to:

if v:IsA("Seat") and v.Name == "LongSeat" then
--- Here: local LastOccupant
v.Changed:Connect(function(Property)

I’ve done that, but it messes up the player’s chat and camera view.

Yes, the animation is uploaded to the group I’m developing for.

Can you send a copy of the game? I can’t promise any fixes quickly, because I feel a bit sick today.

That’s a stupid decision, I’m not going to send a copy of a game that I develop for. It can easily be leaked.

Does LoadAnimation take the Id or does it take an animation instance?
Also you shouldn’t load into the humanoid. Inside the humanoid is the “Animator” which you can load it to.
Its more direct and efficient.
Also for your LoadedAnimation variable it should be a local variable since they are faster and more efficient.

-- Variables

local Occupants = {}

-- Code

for _, Seat in pairs(workspace:GetDescendants()) do
	if Seat:IsA("Seat") and Seat.Name == "LongSeat" then
		Seat:GetPropertyChangedSignal("Occupant"):Connect(function()
			if Seat.Occupant then
				Occupants[Seat] = Seat.Occupant
				local FoundOccupant = Seat.Occupant
				local Animation = Seat.Animation
				local LoadedAnimation = FoundOccupant.Parent:FindFirstChildOfClass("Humanoid"):LoadAnimation(Animation)
				LoadedAnimation:Play()
			else
				if Occupants[Seat] then
					for _, Anim in pairs(Occupants[Seat].Parent.Humanoid:GetPlayingAnimationTracks()) do
						if Anim.Name == "Animation" then
							Anim:Stop()
						end
					end
				end
				Occupants[Seat] = nil
			end
		end)
	end
end

Also I edited your script if u still need it. It works now.

Another update, it wasn’t my script. It was the animator who didn’t set the animation to priority & didn’t loop it. Thank you all for the help, though!