Need help with boat pysics

so i created a boat with movement working but when i touch it or somtimes when i leave it it move in its own boat will become a tool so each player have a uniqeu boat name so its named boatplr123 where 123 will be the user id can somoane pls help me out

local UIS = game:GetService("UserInputService")
local RS = game:GetService("RunService")

local plr = game:GetService("Players").LocalPlayer

local seat = game.Workspace.boatPlr1234.Vseat
local boat = seat.Parent

if not boat.PrimaryPart then
	boat.PrimaryPart = boat.prim
end

local MAX_SPEED = 50
local ACCELERATION = 10  -- Smooth acceleration
local TURN_SPEED = 2 -- Rotation speed

local currentSpeed = 0
local moveDirection = 0 -- Forward/Backward
local turnDirection = 0 -- Left/Right
local targetRotation = boat.PrimaryPart.Orientation.Y

local waterLevel = boat.PrimaryPart.Position.Y -- Keep boat on water

-- Function to update movement
local function updateBoatMovement(dt)
	-- Get forward vector
	local boatCF = boat.PrimaryPart.CFrame
	local forward = boatCF.LookVector

	-- Apply acceleration (gradual increase in speed)
	if moveDirection ~= 0 then
		currentSpeed = math.clamp(currentSpeed + (moveDirection * ACCELERATION * dt), -MAX_SPEED, MAX_SPEED)
	else
		currentSpeed = currentSpeed * 0.98 -- Smooth stopping when key is released
	end

	-- Apply movement
	local newPosition = boatCF.Position + forward * currentSpeed * dt
	newPosition = Vector3.new(newPosition.X, waterLevel, newPosition.Z) -- Keep on water

	-- Apply turning
	targetRotation = targetRotation + (turnDirection * TURN_SPEED)
	local newRotation = CFrame.Angles(0, math.rad(targetRotation), 0)

	-- Set new position and rotation
	boat.PrimaryPart.CFrame = CFrame.new(newPosition) * newRotation
end

-- Detect key presses
local function onInputBegan(input, gameProcessed)
	if gameProcessed then return end
	if seat.Occupant then
		local humanoid = seat.Occupant
		if humanoid and humanoid:IsA("Humanoid") then
			if input.KeyCode == Enum.KeyCode.W then
				moveDirection = moveDirection + 1  -- Add 1 to move forward
			elseif input.KeyCode == Enum.KeyCode.S then
				moveDirection = moveDirection - 1  -- Subtract 1 to move backward
			elseif input.KeyCode == Enum.KeyCode.A then
				turnDirection = turnDirection + 1  -- Turn left (decrease angle)
			elseif input.KeyCode == Enum.KeyCode.D then
				turnDirection = turnDirection - 1  -- Turn right (increase angle)
			end
		end
	end
end

local function onInputEnded(input, gameProcessed)
	if gameProcessed then return end

	if seat.Occupant then
		if input.KeyCode == Enum.KeyCode.W then
			moveDirection = moveDirection - 1  -- Stop moving forward when W is released
		elseif input.KeyCode == Enum.KeyCode.S then
			moveDirection = moveDirection + 1  -- Stop moving backward when S is released
		elseif input.KeyCode == Enum.KeyCode.A then
			turnDirection = turnDirection - 1  -- Stop turning left when A is released
		elseif input.KeyCode == Enum.KeyCode.D then
			turnDirection = turnDirection + 1  -- Stop turning right when D is released
		end
	end
end

local function onGuiButtonClick(button)
	if button.Name == "forw" then
		moveDirection = moveDirection + 1  -- Move forward
	elseif button.Name == "backw" then
		moveDirection = moveDirection - 1  -- Move backward
	elseif button.Name == "left" then
		turnDirection = turnDirection + 1  -- Turn left
	elseif button.Name == "right" then
		turnDirection = turnDirection - 1  -- Turn right
	end
end

-- Function to stop movement when button is released
local function onGuiButtonRelease(button)
	if button.Name == "forw" or button.Name == "backw" then
		moveDirection = 0  -- Stop moving forward/backward
	elseif button.Name == "left" or button.Name == "right" then
		turnDirection = 0  -- Stop turning
	end
end

local gui = plr.PlayerGui.tools.boat.mobile
gui.forw.MouseButton1Down:Connect(function()
	onGuiButtonClick(gui.forw)
end)
gui.backw.MouseButton1Down:Connect(function()
	onGuiButtonClick(gui.backw)
end)
gui.left.MouseButton1Down:Connect(function()
	onGuiButtonClick(gui.left)
end)
gui.right.MouseButton1Down:Connect(function()
	onGuiButtonClick(gui.right)
end)

-- Connect button release events
gui.forw.MouseButton1Up:Connect(function()
	onGuiButtonRelease(gui.forw)
end)
gui.backw.MouseButton1Up:Connect(function()
	onGuiButtonRelease(gui.backw)
end)
gui.left.MouseButton1Up:Connect(function()
	onGuiButtonRelease(gui.left)
end)
gui.right.MouseButton1Up:Connect(function()
	onGuiButtonRelease(gui.right)
end)
gui.esc.MouseButton1Click:Connect(function()
	if seat.Occupant then
		local humanoid = seat.Occupant
		if humanoid and humanoid:IsA("Humanoid") then
			humanoid.Sit = false  -- Unseat the player by setting Sit to false
		end
	end
end)
-- Ensure boat doesn't move randomly due to physics
local function preventRandomMovement()
	-- Stop physics interaction when no input is detected
	if moveDirection == 0 then
		boat.PrimaryPart.AssemblyLinearVelocity = Vector3.zero
		boat.PrimaryPart.AssemblyAngularVelocity = Vector3.zero
	end
end

local function updateGui()
	if seat.Occupant then
		if UIS.TouchEnabled then
			-- disable touch controls
			plr.PlayerGui.TouchGui.Enabled = false
			plr.PlayerGui.tools.boat.mobile.Visible = true
		elseif UIS.KeyboardEnabled then
			plr.PlayerGui.tools.boat.desktop.Visible = true
		end
	else
		if UIS.TouchEnabled then
			-- disable touch controls
			plr.PlayerGui.TouchGui.Enabled = true
			plr.PlayerGui.tools.boat.mobile.Visible = false
		elseif UIS.KeyboardEnabled then
			plr.PlayerGui.tools.boat.desktop.Visible = false
		end
	end
end

-- Connect input events
UIS.InputBegan:Connect(onInputBegan)
UIS.InputEnded:Connect(onInputEnded)

-- Update movement every frame
RS.RenderStepped:Connect(function(dt)
	updateBoatMovement(dt)
	preventRandomMovement()
	updateGui()
end)

Is the problem that when you leave it while holding a key it will keep moving in that direction?

You can probably just have an if statement around updateBoatMovement(dt) that makes sure seat.Occupant ~= nil.

Other than that, I wouldn’t rely on always getting an InputEnded event for each InputBegan event. Personally, I would use UserInputService:GetKeysPressed() to update the moveDirection and turnDirection each time either InputEnded or InputBegan happen. That way it can’t get out of sync with the keys like it can if you miss an event (for example because of the gameProcessed debounce or because the user moved to a different window while holding a key).