Seat script problem

—I know a lot use this script for the animation sit, but i don’t understand why it won’t work, roblox script analysis said that the error is the humanoid----
if somone know how fix it it would be so appreciate it!

seat = script.Parent
function added(child)
if (child.className==“Weld”) then
humanoid = child.part1.Parent:FindFirstChild(“Humanoid”)
if humanoid ~= nil then
anim = humanoid: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)

What do you mean it won’t work? Could you elaborate further?

1 Like

Yes of course, so this script in the script analysis “Warning” it say humanoid is not valide, if you know how to fix this i would be so much happy!

I personally use the exact same script you sent and it works perfectly.
Script analysis gives you that warning because you didn’t prefix the humanoid variable with “local”.

Other than that it will work as long as you put the script in the seat. Please try testing it first, since you said it “won’t work” I’m assuming you posted after seeing the warning.

1 Like

My mistake I forgot to write sitanim in the animation but in the rest it’s ok, but for the warning I can replace humanoid with local?

First, I recommend that you use text formatting when writing code here on the devforum, this way it will be easier for other developers who want to help you to see the code.

seat = script.Parent
function added(child)
if (child.className==“Weld”) then
humanoid = child.part1.Parent:FindFirstChild(“Humanoid”)
if humanoid ~= nil then
anim = humanoid: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)

As for your code, I assume you’ve already solved it, but I think it’s always good to improve some things like the method you’re using to get the Humanoid from a seat and the organization of the code. Below I use the Occuppant method present in VehicleSeat and Seat, this value gives exactly the humanoid of the player who is occupying the current seat, so it is enough to obtain the result of this value to know if there is someone in the seat.

--// Script
local Seat = script.Parent

--// Seat
local SitAnimation = Seat:FindFirstChild('sitanim')

--// Variables
local AnimationTrack

--// Functions
function ChildChanged(child: Instance)
    if not child:IsA('Weld') then return end
    
    if Seat.Occupant then
        AnimationTrack = Seat.Occupant:LoadAnimation(SitAnimation)
        AnimationTrack:Play()
    elseif AnimationTrack then
        AnimationTrack:Stop()
    end
end

--// Connections
Seat.ChildAdded:Connect(ChildChanged)
Seat.ChildRemoved:Connect(ChildChanged)
1 Like

You can write “local humanoid” instead of just “humanoid” and the warning will go away, also since the issue is resolved you should probably close the thread.

1 Like

Thank you, I know it’s just the first time i’m using the devforum and to be honest I’m learning a lot of things from you and other developers!

1 Like

Mhm! Thank you so much again! You are amazing🤩

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