Help with my seat code

  1. I want my code to be able to spawn players in a seat everytime they join the game or respawn. What I also want it to do is spawn a player in another seat if that seat is taken but if all seats are taken I want the player to spawn on top of one of the player sitting in the seat
    I used an AI bot to help code this and I am here to ask help. I aswell looked on other sites to see if I could get help but it was nothing.

  2. I join the game but it does not sit my chracter and it spawns me back in forth between the 2 seats. and if I jump and move my chracter it spawns me right back there doing the same thing but sits me instead.
    ezgif.com-gif-maker (1)
    if the gif is to fast download it and slow it down in a video editor

  3. The solutions on the dev hub did not work. and I looked online and other places but still no help

I have sent the code for you to help me out. I am not that good at coding so I might not understand it all. Also let me show you my objects and where I placed the code so you know what your looking for.

Screenshot_2

local Players = game:GetService("Players")
local LocalPlayer = Players.LocalPlayer
 
-- Replace "Seat1" and "Seat2" with the names of the seats you want to use
local Seats = {
	game.Workspace.Couch1.Seat1,
	game.Workspace.Couch1.Seat2,
}
 
-- This function returns true if the given seat is taken by another player
local function IsSeatTaken(seat)
	for _, player in pairs(Players:GetPlayers()) do
		if player.Character and player.Character:IsDescendantOf(seat) then
			return true
		end
	end
	return false
end
 
-- This function returns a random seat that is not taken by another player
local function GetRandomAvailableSeat()
	local availableSeats = {}
	for _, seat in pairs(Seats) do
		if not IsSeatTaken(seat) then
			table.insert(availableSeats, seat)
		end
	end
	if #availableSeats > 0 then
		return availableSeats[math.random(#availableSeats)]
	end
	return nil
end
 
-- This function returns the seat that the player's character is currently sitting in
local function GetPlayerSeat()
	for _, seat in pairs(Seats) do
		if LocalPlayer.Character and LocalPlayer.Character:IsDescendantOf(seat) then
			return seat
		end
	end
	return nil
end
 
-- This function moves the player's character to the given seat
local function SitInSeat(seat)
	LocalPlayer.Character.HumanoidRootPart.CFrame = seat.CFrame
	LocalPlayer.Character.Humanoid.Sit = true
end
 
-- Main script
while true do
	-- Check if the player is already sitting in a seat
	local seat = GetPlayerSeat()
	if seat then
		-- If the player is already sitting in a seat, check if there are any other seats available
		local availableSeat = GetRandomAvailableSeat()
		if availableSeat then
			-- If there is an available seat, move the player to it
			SitInSeat(availableSeat)
		else
			-- If all seats are taken, move the player on top of another player's seat
			local takenSeat = Seats[math.random(#Seats)]
			SitInSeat(takenSeat)
		end
	else
		-- If the player is not sitting in a seat, try to find an available seat and move the player to it
		local availableSeat = GetRandomAvailableSeat()
		if availableSeat then
			SitInSeat(availableSeat)
		end
	end
	wait(1)
end
LocalPlayer.Character.HumanoidRootPart.Anchored = true
LocalPlayer.Character.HumanoidRootPart.CanCollide = false
 
local position = Vector3.new(-22.862, 1.588, -182.576)  -- position of the seat
local orientation = CFrame.fromEulerAnglesXYZ(math.pi/0, 90, 0)  -- orientation of the seat
 
workspace.Couch1.Seat1.CFrame = CFrame.new(position) * orientation
 
 
local position = Vector3.new(-22.862, 1.588, -187.424)  -- position of the seat
local orientation = CFrame.fromEulerAnglesXYZ(math.pi/0, 90, 0)  -- orientation of the seat
 
workspace.Couch1.Seat2.CFrame = CFrame.new(position) * orientation
 
-- This function moves the player's character to the given seat
local function SitInSeat(seat)
	if not LocalPlayer.Character.HumanoidRootPart then
		return
	end
 
	-- Anchor player to seat and disable collision
	LocalPlayer.Character.HumanoidRootPart.Anchored = true
	LocalPlayer.Character.HumanoidRootPart.CanCollide = false
 
	-- Set the player's character's position and orientation to match the seat
	local position = seat.Position  -- position of the seat
	local orientation = seat.CFrame  -- orientation of the seat
	LocalPlayer.Character.HumanoidRootPart.CFrame = CFrame.new(position) * orientation
 
	-- Set the player's character to sit
	LocalPlayer.Character.Humanoid.Sit = true
end
-- This function moves the player's character to the given seat
local function SitInSeat(seat)
	if not LocalPlayer.Character.HumanoidRootPart then
		return
	end
 
	print("Seat CFrame:", seat.CFrame)  -- print the CFrame of the seat
	print("Character CFrame:", LocalPlayer.Character.HumanoidRootPart.CFrame)  -- print the CFrame of the character
 
	-- Anchor player to seat and disable collision
	LocalPlayer.Character.HumanoidRootPart.Anchored = true
	LocalPlayer.Character.HumanoidRootPart.CanCollide = false
 
	-- Set the player's character's position and orientation to match the seat
	local position = seat.Position  -- position of the seat
	local orientation = seat.CFrame  -- orientation of the seat
	LocalPlayer.Character.HumanoidRootPart.CFrame = CFrame.new(position) * orientation
 
	print("Character CFrame after move:", LocalPlayer.Character.HumanoidRootPart.CFrame)  -- print the CFrame of the character after it is moved
 
	-- Set the player's character to sit
	LocalPlayer.Character.Humanoid.Sit = true
end

Hope you can help. Also im new to posting here so Idk if im doing this right.

here is a script i made and tested, it should work. btw put it inside a script in ServerScriptService

local Seats = {
	game.Workspace.Couch1.Seat1,
	game.Workspace.Couch1.Seat2,
}
local Players = game:GetService("Players")

function SeatOccupied(Seat:Seat)
	local weld:Weld = Seat:FindFirstChild("SeatWeld")
	if weld then
		return weld
	end
end

Players.PlayerAdded:Connect(function(plr)
	plr.CharacterAdded:Connect(function(character)
		spawn(function()
			local hrp = character:WaitForChild("HumanoidRootPart")
			local AvailableSeats = {}
			for _,v in pairs(Seats) do
				if not SeatOccupied(v) then
					table.insert(AvailableSeats,v)
				end
			end
			local PickedSeat
			if #AvailableSeats > 0 then
				PickedSeat = AvailableSeats[math.random(1,#AvailableSeats)]
				hrp.CFrame = PickedSeat.CFrame
			else
				PickedSeat = Seats[math.random(1,#Seats)]
				hrp.CFrame = PickedSeat.CFrame * CFrame.new(0,SeatOccupied(PickedSeat).Part1.Size.Y + 1 + hrp.Size.Y,0)
			end
		end)
	end)
end)

does the exact same thing as before am i doing something wrong

not sure, when i tested the script it worked completely fine. maybe somehow another script is doing something to the character

I only have 1 script. Do I need to rename anything


in the script it does show orange

Oh sorry i forgot to update the script i edited it after i noticed it was a little broken uhh maybe this should work if it doesnt im really confused on why

local Seats = {
	game.Workspace.Couch1.Seat1,
	game.Workspace.Couch1.Seat2,
}
local Players = game:GetService("Players")

function SeatOccupied(Seat)
	local weld = Seat:FindFirstChild("SeatWeld")
	if weld then
		return weld
	end
end

Players.PlayerAdded:Connect(function(plr)
	plr.CharacterAdded:Connect(function(character)
		spawn(function()
			local hrp = character:WaitForChild("HumanoidRootPart")
			local AvailableSeats = {}
			for _,v in pairs(Seats) do
				if not SeatOccupied(v) then
					table.insert(AvailableSeats,v)
				end
			end
			local PickedSeat
			if #AvailableSeats > 0 then
				PickedSeat = AvailableSeats[math.random(1,#AvailableSeats)]
				hrp.CFrame = PickedSeat.CFrame
			else
				PickedSeat = Seats[math.random(1,#Seats)]
				hrp.CFrame = PickedSeat.CFrame * CFrame.new(0,SeatOccupied(PickedSeat).Part1.Size.Y + 1 + hrp.Size.Y,0)
			end
		end)
	end)
end)

do I remove the original script i own

yeah this script will replace it. btw you can change the Seat table to have the seats you want

thanks so much it works and also if i was able to add more seats and stuff do i just add on to the
game.workspace.2ndcouchname.2ndseatname and it would still work

and sorry to ask to much but is there away to where i can make it like spawn me right away instead of me falling outside the place then spawning there. I dont want people in my game to see the outside of map

yea u can add more seats to that like this


local Seats = {

workspace.TestSeat1,

workspace.TestSeat2,

workspace.TestSeat3,

-- and add some more if u want just dont forget the comma at the end of each of them

}

it might be because the script is on the server side and client responds a little late to the teleport but i could make it local script

you can do that but i dont want to waiste your time

oh ok well i hope your game development goes well

and quick question if i was to make it a local script i just paste it in there right and it works like that

It would need a little modification but fortunately this one isnt hard to change for local script so here

local Seats = {
	game.Workspace.Couch1.Seat1,
	game.Workspace.Couch1.Seat2,
}
local character = script.Parent
local hrp = character:WaitForChild("HumanoidRootPart")

function SeatOccupied(Seat)
	local occupant = Seat.Occupant
	return occupant
end

function teleportToSeat()
	local AvailableSeats = {}
	for _,v in pairs(Seats) do
		if not SeatOccupied(v) then
			table.insert(AvailableSeats,v)
		end
	end
	local PickedSeat
	if #AvailableSeats > 0 then
		PickedSeat = AvailableSeats[math.random(1,#AvailableSeats)]
		hrp.CFrame = PickedSeat.CFrame
	else
		PickedSeat = Seats[math.random(1,#Seats)]
		hrp.CFrame = PickedSeat.CFrame * CFrame.new(0,SeatOccupied(PickedSeat).Parent.PrimaryPart.Size.Y + 1 + hrp.Size.Y,0)
	end
end

teleportToSeat()

btw put this inside a local script in StarterPlayer>StarterCharacterScripts

I will try right now to see if it works thanks

doing that code just tp me out of map and does nothing else

wait hold on i forgot to fix something