Random Sitting Animations Error

I have this error for whenever someone sits on a ‘Seat’ object, the pose that has been set for the seat keeps playing even after the player has gotten up from it.

Error code: “Argument 1 missing or nil”

I’ve surfed through the internet for some solutions but I’ve found none for this specific error.

Here is the script I use:

seat = script.Parent

-- Name of the folder that contains the sitting poses
local SITTING_POSES_FOLDER_NAME = "SittingPoses"

function added(child)
	if child.ClassName == "Weld" then
		local character = child.Part1.Parent
		if character:FindFirstChild("Humanoid") then
			local posesFolder = game.Workspace:FindFirstChild(SITTING_POSES_FOLDER_NAME)
			if posesFolder and posesFolder:IsA("Folder") then
				local poses = posesFolder:GetChildren()
				local sittingPose = poses[math.random(#poses)]
				character.Humanoid:LoadAnimation(sittingPose):Play()
			end
		end
	end
end

function removed(child)
	if child.ClassName == "Weld" then
		local character = child.Part1.Parent
		if character:FindFirstChild("Humanoid") then
			character.Humanoid:LoadAnimation():Stop()
		end
	end
end

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

This script is supposed to play a random sitting pose every time someone sits on it, those animations are randomly chosen out from a folder.

The error is said to be on the 24th line, though I’m not sure how to fix it.

LoadAnimation always needs an Animation object, to stop the AnimationTrack after getting up you can save the object it returns

local seat = script.Parent

-- Name of the folder that contains the sitting poses
local SITTING_POSES_FOLDER_NAME = "SittingPoses"
local LastAnimation = {}

local function added(child)
	if child.ClassName == "Weld" then
		local character = child.Part1.Parent
		if character:FindFirstChild("Humanoid") then
			local posesFolder = workspace:FindFirstChild(SITTING_POSES_FOLDER_NAME)
			if posesFolder and posesFolder:IsA("Folder") then
				local poses = posesFolder:GetChildren()
				local sittingPose = poses[math.random(#poses)]
				LastAnimation[character.Humanoid] = character.Humanoid:LoadAnimation(sittingPose)
				LastAnimation[character.Humanoid]:Play()
			end
		end
	end
end

local function removed(child)
	if child.ClassName == "Weld" then
		local character = child.Part1.Parent
		if character:FindFirstChild("Humanoid") and LastAnimation[character.Humanoid] then
			LastAnimation[character.Humanoid]:Stop()
			LastAnimation[character.Humanoid] = nil
		end
	end
end
seat.ChildAdded:Connect(added)
seat.ChildRemoved:Connect(removed)

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