Sync player movement with a Moving Ship

Normally I don’t find the need to ask for help on scripting but this really has me stumped I am trying to figure out how to synchronize players with a ship that they are on. Roblox physics almost does the job but it fails when they are in the air or jumping… SO I realize the CFraming via local script is most likely the solution but idk really know how to do that wondering if anyone can help with this?

This is my ship detection logic:

local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local hrp = character:WaitForChild(“HumanoidRootPart”)
local shipsFolder = workspace:WaitForChild(“Ships”)
local closestShip = nil

– Function to check if the player is inside the ship’s hitbox
local function isPlayerInsideShip(ship)
local hitbox = ship:FindFirstChild(“Hitbox”)
if hitbox then
local pos = hitbox.Position
local size = hitbox.Size
local region = Region3.new(pos - size/2, pos + size/2)
local parts = workspace:FindPartsInRegion3(region, nil, math.huge)
for _, part in ipairs(parts) do
if part == hrp then
return true
end
end
end
return false
end

– Find the closest ship in range
local function findClosestShip()
closestShip = nil
local closestDistance = math.huge
for _, ship in pairs(shipsFolder:GetChildren()) do
if ship:IsA(“Model”) and ship:FindFirstChild(“PrimaryPart”) then
if isPlayerInsideShip(ship) then
local d = (hrp.Position - ship.PrimaryPart.Position).Magnitude
if d < closestDistance then
closestDistance = d
closestShip = ship
end
end
end
end
end

– Function to move the player with the ship when airborne (horizontal velocity sync)
local function moveWithShip()
if closestShip and closestShip:FindFirstChild(“PrimaryPart”) then
–CFrame with ship should occure here
print("moving with: "…closestShip)
end
end

– Update every frame
game:GetService(“RunService”).Heartbeat:Connect(function()
findClosestShip() – Find closest ship
if closestShip then
– Apply the ship’s velocity to the player when airborne (no interference with jumping)
moveWithShip()
end
end)
robloxapp-20250307-1530142.wmv (4.2 MB)

Please format the text it would help


local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local hrp = character:WaitForChild(“HumanoidRootPart”)
local shipsFolder = workspace:WaitForChild(“Ships”)
local closestShip = nil

– Function to check if the player is inside the ship’s hitbox
local function isPlayerInsideShip(ship)
local hitbox = ship:FindFirstChild(“Hitbox”)
if hitbox then
local pos = hitbox.Position
local size = hitbox.Size
local region = Region3.new(pos - size/2, pos + size/2)
local parts = workspace:FindPartsInRegion3(region, nil, math.huge)
for _, part in ipairs(parts) do
if part == hrp then
return true
end
end
end
return false
end

– Find the closest ship in range
local function findClosestShip()
closestShip = nil
local closestDistance = math.huge
for _, ship in pairs(shipsFolder:GetChildren()) do
if ship:IsA(“Model”) and ship:FindFirstChild(“PrimaryPart”) then
if isPlayerInsideShip(ship) then
local d = (hrp.Position - ship.PrimaryPart.Position).Magnitude
if d < closestDistance then
closestDistance = d
closestShip = ship
end
end
end
end
end

– Function to move the player with the ship when airborne (horizontal velocity sync)
local function moveWithShip()
if closestShip and closestShip:FindFirstChild(“PrimaryPart”) then
–CFrame with ship should occure here
print("moving with: "…closestShip)
end
end

– Update every frame
game:GetService(“RunService”).Heartbeat:Connect(function()
findClosestShip() – Find closest ship
if closestShip then
– Apply the ship’s velocity to the player when airborne (no interference with jumping)
moveWithShip()
end
end)

Oh sorry, yeah idk how to do that here… I never used this before

local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local hrp = character:WaitForChild("HumanoidRootPart")
local shipsFolder = workspace:WaitForChild("Ships")
local closestShip = nil

--Function to check if the player is inside the ship’s hitbox
	local function isPlayerInsideShip(ship)
		local hitbox = ship:FindFirstChild("Hitbox")
		if hitbox then
			local pos = hitbox.Position
			local size = hitbox.Size
			local region = Region3.new(pos - size/2, pos + size/2)
			local parts = workspace:FindPartsInRegion3(region, nil, math.huge)
			for _, part in ipairs(parts) do
				if part == hrp then
					return true
				end
			end
		end
		return false
	end

	-- Find the closest ship in range
	local function findClosestShip()
		closestShip = nil
		local closestDistance = math.huge
		for _, ship in pairs(shipsFolder:GetChildren()) do
			if ship:IsA("Model") and ship:FindFirstChild("PrimaryPart") then
				if isPlayerInsideShip(ship) then
					local d = (hrp.Position - ship.PrimaryPart.Position).Magnitude
					if d < closestDistance then
						closestDistance = d
						closestShip = ship
					end
				end
			end
		end
	end

	--Function to move the player with the ship when airborne (horizontal velocity sync)
	local function moveWithShip()
		if closestShip and closestShip:FindFirstChild("PrimaryPart") then
			--CFrame with ship should occure here
			print("moving with: "..closestShip)
		end
	end

	--Update every frame
	game:GetService("RunService").Heartbeat:Connect(function()
		findClosestShip() --Find closest ship
		if closestShip then
			--Apply the ship’s velocity to the player when airborne (no interference with jumping)
			moveWithShip()
		end
	end)