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
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")
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 )
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
-- 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
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
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
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