Need Help with Animation Load Error

I have a script that plays animations on multiple seats with unique animations inside it but I’m encountering an error and need some assistance. The error message I’m receiving is “Unable to cast value to Object.”

-- Server Script in ServerScriptService

-- Find the folder containing the seats and animations
local sitTestFolder = game.Workspace:WaitForChild("SitTest")

-- Loop through all seats in the folder
for _, seat in pairs(sitTestFolder:GetChildren()) do
	if seat.Name:match("^Seat%d+$") then
		-- Check if the seat has an Animation object with AnimationId property
		local sitanim = seat:FindFirstChild("sitanim")
		if sitanim and sitanim:IsA("Animation") then
			-- Connect seat interaction
			seat.Touched:Connect(function(other)
				local character = game.Players:GetPlayerFromCharacter(other.Parent)
				if character then
					local humanoid = character.Character:FindFirstChild("Humanoid")
					if humanoid then
						local animationId = sitanim.AnimationId
						local anim = humanoid:LoadAnimation(animationId) -- Error occurs here
						anim:Play()
					end
				end
			end)
		end
	end
end

I would greatly appreciate any guidance or insights on how to resolve this error or make a new script that would basically play their respective AnimationId of all Seats with a single script

6 Likes

try just referencing the animation itself instead of the ID

2 Likes

can u change that line in my script so i could try it please?

1 Like

just remove .animationId in the sitanim

tried that and their are no errors in the output yet the animation does not play on seat

change local anim = humanoid:LoadAnimation(animationId) to local anim = humanoid:LoadAnimation(sitanim)

that kind of worked but it does not stop the animation when the player jumps from the seat and it freezes the players animatio

do anim:Stop() when he stops sitting

oh and apparently it’s better to use animator:LoadAnimation
works the same way, it’s not deprecated
usually you can get the animator by doing humanoid:FindFirstChildOfClass("Animator")

1 Like

put this in after anim:Play()

humanoid.Jumping:Connect(function(check)
if check then
anim:Stop()
end
end)

1 Like

i see , i think i will have to rewrite the whole script as the animations do not seem to play when the player sits on the seat, i have a script that works perfectly but it works for just one seat , so if i have 33 seats ill have to add 33 scripts which would impact the performance , im trying to figure out how do i make it into a single script

seat = script.Parent
function added(child)
	if (child.className=="Weld") then
		human = child.part1.Parent:FindFirstChild("Humanoid")
		if human ~= nil then
			anim = human:LoadAnimation(seat.sitanim)
			anim:Play()
		end
	end
end

function removed(child2)
	if anim ~= nil then
		anim:Stop()
		anim:Remove()
	end
end

seat.ChildAdded:connect(added)
seat.ChildRemoved:connect(removed)

this is the script that works , i dont know how to implement it to multiple seats at once having an unique animation id, i tried using collection tag service and it did not work either… or maybe i could script that with a module script ( which i dont have knowledge about )

1 Like

You can also just make the seat a regular seat and replace the character’s default sitting animation if its not used anywhere else.

if the seats is part of the model, you should do this script as part of model, but if it’s decoration or self-use seats like theatre or something, you should collect them at the begining of the game, and use for loop to play those function for all of them at once, with that performance will be better

1 Like

anyways, here’s what i’d do

-- Server Script in ServerScriptService
local players = game:GetService("Players") -- Players is a Service, it's a good idea to use :GetService
local sitTestFolder = workspace:WaitForChild("SitTest") -- yes there is a "workspace" global, yes it's better to use that

for _, seat in ipairs(sitTestFolder:GetChildren()) do -- ipairs is usually better for arrays, which :GetChildren() and :GetDescendants() always give
	if not seat.Name:match("^Seat%d+$") then continue end -- continue works like return, but the loop won't die, instead it kinda skips one turn
local sitanim = seat:FindFirstChild("sitanim")
	if not sitanim or not sitanim:IsA("Animation") then continue end -- don't nest too much or the giant wall of "end"s and blank spaces will haunt you
	seat.Touched:Connect(function(other)
		local character = players:GetPlayerFromCharacter(other.Parent)
		local humanoid = character and character:FindFirstChildOfClass("Humanoid") -- yep, this is a thing you can do
		local animator = humanoid and humanoid:FindFirstChildOfClass("Animator")
		if animator then
			local anim = animator:LoadAnimation(sitanim) -- use sitanim instead of sitanim.AnimationId
			anim:Play()
			local old, new
			repeat old, new = humanoid.StateChanged:Wait() until new~=Enum.HumanoidStateType.Seated -- wait until the player's not seating
			anim:Stop()
		end
	end)
end
1 Like

well they aren’t a model and i have already collected them in a folder , naming them Seat1,Seat2… Seat33 and each one of those a unique AnimationId , i just dont know how to make them work in a single script , i know how to do it for each seat but that would take 33 scripts and lagg the game

1 Like

just tried that and no errors yet the seat doesn’t play the animation when player sits

this is how it looks inside the workspace

Soo, in this scenario you have to go through folder and then define every object in them as v, get possible animation like v.Animation and then do this functions on them inside this loop, but instead of seat put v, soo everytime player seat on any of those seats that is inside loop, they’ll fire the function for them

for i, v in pairs(game.Workspace:WaitForChild(“SitTest”):GetChildren()) do
if v:FindFirstChild(“sitanim”) then
v.Touched:Connect(function(hit)
local humanoid = hit.Parent:FindFirstChild(“Humanoid”)
if humanoid then
local anim = humanoid:LoadAnimation(v.sitanim)
anim:Play
anim.Jumping:Connect(function(check)
if check then
anim:Stop()
end
end)
end
end)
end
end

2 Likes

you can use ``` to make it looks better, like

local scripts = workspace.script
2 Likes

yeah Im not on the pc rn I know about that

3 Likes

since that one worked

maybe this will do

local function handleSeat(seat)
if not seat.Name:match("^Seat%d+$") then return end
local sitanim = seat:WaitForChild("sitanim", 5)
if not sitanim then return end
local anim
local function added(child)
	if not child:IsA("Weld") then return end
	local human = child.Part1.Parent:FindFirstChildOfClass("Humanoid") -- idk, roblox likes PascalCase instead of camelCase now ig
	if human ~= nil then
		anim = human:LoadAnimation(seat)
		anim:Play()
	end
end

local function removed(child2)
	if anim ~= nil then
		anim:Stop()
		anim:Destroy() -- :Destroy() works better, way less likely for things like memory leaks to happen
	end
end

seat.ChildAdded:Connect(added) -- :Connect() with a capital letter C is newer or something
seat.ChildRemoved:Connect(removed)
end

local seatFolder = workspace:WaitForChild("SeatTest")
seatFolder.ChildAdded:Connect(handleSeat)
for _, seat in ipairs(seatFolder:GetChildren()) do
    task.spawn(handleSeat, seat)
end
1 Like