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