Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How to check how many players are seated?

I want to know how many players are seated, but i fail at it.

local part = script.Parent

part.Touched:Connect(function(hit)
	
if hit.Parent.ArmyTruck then
	print("Army Truck")
	local Players = game.Players
	
for _, player in pairs(Players:GetPlayers()) do
	if not player.Character then
    print("waiting for character")
    player.CharacterAdded:wait();
    char = player.Character
else
    char = player.Character
	end
	
if char.Humanoid.Seated == true then
	print("Waiting for other players")
else
	print("No one is seating")
end	
	
	
end
	
end
end)

1 comment

0

try char:FindFirstChild("Humanoid").Seated ~= nil then -- code end

1 answer

✓ Accepted answer

Loop through the seats and check if there is a humanoid in the occupant and insert the characters sitting on them into a table, and then get the number of objects in that table.


local part = script.Parent

local Occupants = {}

part.Touched:Connect(function(hit)
	if hit.Parent.ArmyTruck then
		print("Army Truck")

		for i,v in pairs(hit.Parent.ArmyTruck:GetChildren()) do 
			if v:IsA("VehicleSeat") then
				if v.Occupant ~= nil then
					table.insert(Occupants,v.Occupant.Parent)
				end
			end
		end

		print("There are "..#Occupants.." on this army truck right now.")
	end
end)

We loop through the children of the army truck and if there is a vehicle seat we proceed to check if there is an occupant and if there is one we insert it into the table "Occupants". We then print the number of people in occupants afterwards.

This discussion is preserved as it was. New answers, comments, and votes are closed.