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

how do i print("something") only when player sits in these 5 seats?

question looks complex but problem is actually fairly simple but i just dont get it

so these 5 seats are named a,b,c,x,y and is in a folder in the workspace

i want the game to print something when the player sits in them

heres my code it didnt work

local GetSeat = workspace.GetSeat
local Seats = GetSeat:GetChildren()

Seats.ChildAdded:Connect(function()
	print("w")
end)

i didnt wanna have to reference all of these seats cuz i have a project that requires me to go through 125 seats and im just playing around with these 5 seats for now to solve my problem

i would like something that just detects if the player is sitting in a seat that is in this folder then it will print something

3 answers

So my friend, you may be consulting Seat.occupant if that's what I understand, you may be using the following script:

    local Players = game:GetService("Players")
    
    local seat = Instance.new("Seat")
    seat.Anchored = true 
    seat.Position = Vector3.new(0, 1, 0)
    seat.Parent = workspace 
    
    local currentPlayer = nil
    
    seat:GetPropertyChangedSignal("Occupant"):Connect(function()
    	local humanoid = seat.Occupant 
    	if humanoid then 
			print"Seat"
    	end
    end)

provided by Seat api

Your best bet is since you have so many seats that you could loop through them, and check for an occupant change.

local Seats = workspace.GetSeat:GetChildren()
local Players = game:GetService('Players')
local Count = 0

for _,seat in pairs(Seats) do
	seat:GetPropertyChangedSignal('Occupant'):Connect(function()
		local Character = seat.Occupant.Parent
		if Character then
			local Player = Players:GetPlayerFromCharacter(Character)
			print(Player.Name..' has seated.')
			Count = Count + 1
		else
			if Count > 0 then
				Count = Count - 1
			end -- make sure it stays at 0
		end -- character check

		if Count == 5 then
			-- your code here
		end -- check count per change
	end) -- Occupant changed
end -- for loop

1 comment

0

thanks

i found that this works for me

local Seats = workspace.Ship.Seats:GetChildren()

for x, Children in pairs(Seats) do
	Children.ChildAdded:Connect(function(Child)
		if Child:IsA("Weld") and Child.Name == "SeatWeld" then
			local Character = Child.Part1.Parent
			local Player = game.Players:GetPlayerFromCharacter(Character)
			local ClonedScript = script.LocalScript:Clone()
			ClonedScript.Parent = Player.Character
			
			Children.ChildRemoved:Connect(function()
				ClonedScript:Destroy()
			end)
		end
	end)
end

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