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.
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
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.