Making player sit in a boat doesn't work [Seat:Sit(humanoid)]

I want to make player’s character sit in the boat when they walk on the yellow part.



As you can see, the player gets teleported under the seat which is not supposed to happen.
Here is my code:

-- Boat.server.lua
-- Handles players sitting in the boat
-- SiberianCatKitten

local boat1 = workspace.Lobby.Boat1
local boat2 = workspace.Lobby.Boat2
local enterBoat = workspace.Lobby.EnterBoat

enterBoat.Touched:Connect(function(touch)
	if touch.Parent:FindFirstChild("Humanoid") ~= nil then
		local humanoid = touch.Parent:FindFirstChild("Humanoid")

		for _, seat in pairs(boat2.Seats:GetChildren()) do
			local isOccupied = seat:FindFirstChild("IsOccupied").Value
			if not isOccupied then
				humanoid.Parent:FindFirstChild("HumanoidRootPart").Position = seat.Position
				seat:Sit(humanoid)
				isOccupied = true
			end
		end
	end
end)

I have tried looking from the developer hub and according to Seat API Reference, humanoid can be forced to sit on the seat by using function Seat:Sit(humanoid). I don’t know why it doesn’t work.

Also, it would be useful if you could tell me how to prevent the player from getting up once they sit on the seat.

1 Like

Try removing this line:

humanoid.Parent:FindFirstChild("HumanoidRootPart").Position = seat.Position

Now the player’s character doesn’t do anything when touching enterBoat part.

change it to thise, change the Vector3.new() y value until it put the character where you want it

humanoid.Parent:FindFirstChild("HumanoidRootPart").Position = seat.Position + Vector3.new(0, 1, 0)

as for keeping the player in the seat, you could try something like

seat.Changed:Connect(function(property)
		if property	== "Occupant" and not seat.Occupant then
			--code to put the player back in the seat
		end
end)

Maybe make the bench non collide able. I only say this because you spawn kinda under the bench and with the bench non collide able to should theoretically lock you into the seat until you active an exit type strategy like jump.

You should be able to make the character sit on the seat regardless of proximity. Try implementing a debounce on your touched script. Also your for loop will keep running despite the character already sitting, you must add a break if the character has been sitted already.

Edit:
Here’s to demonstrate what I’m saying

local Workspace = game:GetService("Workspace")

local SeatsFolder = Workspace:WaitForChild("Seats")
local Sensor = Workspace:WaitForChild("Sensor")

local Debounce = false

local function OnTouched(Hit)
	if not Debounce then
		Debounce = true
		local Humanoid = Hit.Parent:FindFirstChild("Humanoid")
		if Humanoid then
			for i, v in ipairs(SeatsFolder:GetChildren()) do
				print(i)
				if v.Occupant == nil then --we found a vacant seat
					v:Sit(Humanoid) --so we sit the character
					break --then we stop the loop from continuing
				end
			end
			wait(3)
			Debounce = false
		end
	end
end

Sensor.Touched:Connect(OnTouched)

In this code, as soon as the character has been seated, we stop the loop, otherwise the code wont work due to the excess calls to :Sit

Compare it to this:

local Workspace = game:GetService("Workspace")

local SeatsFolder = Workspace:WaitForChild("Seats")
local Sensor = Workspace:WaitForChild("Sensor")

local Debounce = false

local function OnTouched(Hit)
	if not Debounce then
		Debounce = true
		local Humanoid = Hit.Parent:FindFirstChild("Humanoid")
		if Humanoid then
			for i, v in ipairs(SeatsFolder:GetChildren()) do
				print(i)
				v:Sit(Humanoid)
			end --we dont stop the loop from continuing
            wait(3)
            Debounce = false
		end
	end
end

Sensor.Touched:Connect(OnTouched)

This won’t work because we didnt stop the loop and excess calls to :Sit will be made

You could just do:

Seat:Sit(Humanoid)

Instead of teleporting them to the seat.

This won’t solve the underlying issue which is the excess calls to :Sit. Making the bench non-collidable will just make the seat resort to default behavior (touch the seat to sit) however the OP wants to use Seat:Sit(Humanoid). The issue here is the loop going through each seat doesn’t stop when the character has already been assigned to a seat, hence the call will get cancelled out.

1 Like

To prevent the player from jumping, just set Humanoid.JumpPower to 0.