Moving boat with AlignPosition

I was trying to make a boat system but got stuck with even moving it forward/backward and also having a problem with it flipping over

Server Script:

local Boat = script.Parent
local VehicleSeat = Boat.VehicleSeat

local AlignPos = Boat.PrimaryPart.AlignPosition

VehicleSeat:GetPropertyChangedSignal("Throttle"):Connect(function()
	local x = AlignPos.Position.X
	local y = AlignPos.Position.Y
	local z = AlignPos.Position.Z	

	AlignPos.Position = Vector3.new(
		x,
		y,
		z * -VehicleSeat.Throttle * 0.5
	)
end)

Model:
image

Problem

  • It moves forward but how can i do it for backwards too?
  • flips boat if the player jumps on the edge of the boat
  • Only goes in one direction and doesn’t stop until it reaches its destination
2 Likes

There are so many posts about boats already. Try the Search button up top.

Are you using Terrain Water? I’ve got quite a few solutions on boat topics that I’ve posted on to keep the boat from flipping.
If you aren’t using Water then you need an AlignOrientation to keep it level. You can also use it to steer the boat.

1 Like

Don’t multiply Throttle with z because if Throttle becomes zero – it literally makes z go to 0 (which explains why it just “goes forward”)

Do addition instead.

AlignPos.Position = Vector3.new(
	x,
	y,
	z + (-VehicleSeat.Throttle * 5)
)

Also, just put the script in the seat and use vehicleSeat = script.Parent.
It’s a good idea to not use actual names of items as Variable names. Just make the capital letter a small case letter.

yeah i already figured that out but thank you

I did theres none that helped me

You didn’t answer my Terrain/not Terrain question though.

no its not terrain its custom water/just a part

I searched ‘boat on Part water’, ‘boat using Part water’, ‘boat floating on Part’, and found quite a few posts that may help.

1 Like

send them if you think they might help

You can search them too. Some have solved checkmarks so look at them first.

Honestly I’d just rewrite the code its messy and rather complicated. My version, where the AlignPosition continues to use OneAttachment

local seat = script.Parent -- Script is in vehicle seat
local boat = seat.Parent.PrimaryPart -- Boat model's main part
local ap = boat.AlignPosition -- Align position which is connected to a attachment in the boat's main part
local isholding; local speed = 10
seat:GetPropertyChangedSignal("ThrottleFloat"):Connect(function() -- Throttle is deprecated
	local tf = seat.ThrottleFloat
	if tf == 0 then isholding = false else isholding = true end 
	while isholding do
		ap.Position = boat.Position + boat.CFrame.LookVector * speed * tf
		task.wait()
	end
end)

Basically the code operates in a while loop that runs only when the player is holding either movement key. The align position will move parallel to the direction its facing ,speed studs, with tf determining whether it should move forward or back (its either 1 or -1). If the player stops holding (tf = 0), the flag will be false so they’ll stop moving. The same logic can be used for steering

thank you also i did not know throttle was deprecated