To allow the boat to move forward while turning, we need to ensure that the movement and turning commands are handled independently. This means that pressing W (forward) and A or D (turning) simultaneously should result in the boat moving forward and turning at the same time.
We'll update both the LocalScript and the ServerScript to handle this.
Updated LocalScript
The LocalScript will send separate commands for movement and turning, allowing both to be active at the same time.
local UserInputService = game:GetService("UserInputService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local BoatMovementEvent = ReplicatedStorage:WaitForChild("BoatMovementEvent")
local currentBoat = nil
local isSeated = false
-- Detect when seated in a BoatSeat
player.CharacterAdded:Connect(function(char)
char:WaitForChild("Humanoid").Seated:Connect(function(seated, seat)
if seated and seat and seat.Name == "BoatSeat" then
currentBoat = seat:FindFirstAncestorOfClass("Model")
isSeated = true
else
isSeated = false
currentBoat = nil
end
end)
end)
-- Handle key presses
UserInputService.InputBegan:Connect(function(input, gameProcessed)
if gameProcessed or not isSeated or not currentBoat then return end
local key = input.KeyCode
if key == Enum.KeyCode.W then
BoatMovementEvent:FireServer(currentBoat, "Forward")
elseif key == Enum.KeyCode.S then
BoatMovementEvent:FireServer(currentBoat, "Backward")
elseif key == Enum.KeyCode.D then
BoatMovementEvent:FireServer(currentBoat, "Left")
elseif key == Enum.KeyCode.A then
BoatMovementEvent:FireServer(currentBoat, "Right")
end
end)
-- Handle key releases
UserInputService.InputEnded:Connect(function(input, gameProcessed)
if gameProcessed or not isSeated or not currentBoat then return end
local key = input.KeyCode
if key == Enum.KeyCode.W then
BoatMovementEvent:FireServer(currentBoat, "StopMovement")
elseif key == Enum.KeyCode.S then
BoatMovementEvent:FireServer(currentBoat, "StopMovement")
elseif key == Enum.KeyCode.D then
BoatMovementEvent:FireServer(currentBoat, "StopTurning")
elseif key == Enum.KeyCode.A then
BoatMovementEvent:FireServer(currentBoat, "StopTurning")
end
end)
Updated ServerScript
The ServerScript will handle the movement and turning independently, allowing both to be active at the same time.
-- Server Script: Moves the entire boat model via its PrimaryPart.CFrame
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RunService = game:GetService("RunService")
local boatMovementEvent = ReplicatedStorage:WaitForChild("BoatMovementEvent")
local MOVEMENT_SPEED = 12 -- Adjust speed (studs per second)
local TURN_SPEED = 1.5 -- Adjust turn speed (radians per second)
-- Track active boats and their movement states
local boatStates = {} -- Format: { [boatModel] = { moving = "Forward"/"Backward"/"Stop", turning = "Left"/"Right"/"Stop" } }
-- Update boat position/rotation every frame
RunService.Heartbeat:Connect(function(deltaTime)
for boat, state in pairs(boatStates) do
-- Ensure the boat exists and has a PrimaryPart
if not boat or not boat:IsDescendantOf(game.Workspace) or not boat.PrimaryPart then
boatStates[boat] = nil -- Clean up if boat is destroyed or invalid
continue
end
local currentCFrame = boat.PrimaryPart.CFrame
local newCFrame = currentCFrame
-- Calculate movement forward/backward (keep Y-level constant)
local moveDirection = Vector3.new(0, 0, 0)
if state.moving == "Forward" then
moveDirection = currentCFrame.LookVector * MOVEMENT_SPEED * deltaTime
elseif state.moving == "Backward" then
moveDirection = -currentCFrame.LookVector * MOVEMENT_SPEED * deltaTime
end
-- Lock Y-axis movement: Only apply X and Z components
moveDirection = Vector3.new(moveDirection.X, 0, moveDirection.Z)
newCFrame = newCFrame + moveDirection
-- Calculate turning left/right (only rotate around Y-axis)
local turnAngle = 0
if state.turning == "Left" then
turnAngle = -TURN_SPEED * deltaTime
elseif state.turning == "Right" then
turnAngle = TURN_SPEED * deltaTime
end
newCFrame = newCFrame * CFrame.Angles(0, turnAngle, 0)
-- Apply the new CFrame to the boat's PrimaryPart
boat:SetPrimaryPartCFrame(newCFrame)
end
end)
-- Listen for movement commands from clients
boatMovementEvent.OnServerEvent:Connect(function(player, boat, command)
-- Validate the boat and ensure the player is seated in it
if not boat or not boat:IsDescendantOf(game.Workspace) or not boat.PrimaryPart then
warn("Invalid boat or missing PrimaryPart received from client.")
return
end
local seat = boat:FindFirstChild("BoatSeat")
if not seat or seat.Occupant ~= player.Character.Humanoid then
warn(player.Name .. " tried to move a boat they are not seated in or that has no seat.")
return -- Prevent unauthorized movement
end
-- Initialize boat state if not exists
if not boatStates[boat] then
boatStates[boat] = { moving = "Stop", turning = "Stop" }
end
-- Update movement/turn state based on command
local state = boatStates[boat]
if command == "Forward" then
state.moving = "Forward"
elseif command == "Backward" then
state.moving = "Backward"
elseif command == "Left" then
state.turning = "Left"
elseif command == "Right" then
state.turning = "Right"
elseif command == "StopMovement" then
state.moving = "Stop"
elseif command == "StopTurning" then
state.turning = "Stop"
end
end)