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

Checking whenever a player sits on a Seat part, is there a better way to get that player?

Whenever the script.Parent (SeatPart) has a child added, I get the SeatPart's SeatWeld and get that weld's Part1. If that part's Parent is a character of a player, it prints "A player sat down!". Is there a better way to define the player that sat down on the part?

Here's the code:

script.Parent.ChildAdded:Connect(function(part)
	print("Sat down!")
	local part = script.Parent.SeatWeld.Part1
	local plr = part.Parent
	if game.Players:GetPlayerFromCharacter(plr) then 
		print("A player sat down!")
	end
end)

1 answer

✓ Accepted answer

You can access the player who is sitting on the seat through the Occupant value which is located inside the seat like i have done below

local Seat = script.Parent

function SitDetection(Child)
	if Child.Name == "SeatWeld" then
		local Character = Seat.Occupant.Parent
		local Player = game.Players:GetPlayerFromCharacter(Character)
		print(Player.Name)
		print(Character.Name)
	end
end

Seat.ChildAdded:Connect(SitDetection)

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