Moving ship bug

What do you want to achieve?
Being able to jump around while a ship is on the move.

What is the issue?
Jumping while on a moving ship seems to work fine, but when the player stays still, they start to slide towards the front.

Here’s the script:

local Players = game:GetService("Players")
local player = game.Players.LocalPlayer
local RunService = game:GetService('RunService')

local LastHit
local LastTrainCFrame

local Function
local Function2

local Whitelist = {workspace.Orion["Cruiser Kappa"]}

Function = RunService.Heartbeat:Connect(function()
	local RootPart = player.Character.HumanoidRootPart
	local Ignore = player.Character
	local ray = Ray.new(RootPart.CFrame.p, Vector3.new(0,-50,0))
	local Hit, Position, Normal, Material
	if LastHit then
		Hit, Position, Normal, Material = workspace:FindPartOnRayWithWhitelist(ray,{LastHit})
		if not Hit then
			Hit, Position, Normal, Material = workspace:FindPartOnRayWithWhitelist(ray,Whitelist)
		end
	else
		Hit, Position, Normal, Material = workspace:FindPartOnRayWithWhitelist(ray,Whitelist)
	end
	if Hit then
		local Train = Hit
		if not LastTrainCFrame then
			LastTrainCFrame = Train.CFrame
		end
		local TrainCF
		if Train ~= LastHit and LastHit then
			TrainCF = LastHit.CFrame
		else
			TrainCF = Train.CFrame
		end
		local Rel = TrainCF * LastTrainCFrame:inverse()
		LastTrainCFrame = Train.CFrame
		RootPart.CFrame = Rel * RootPart.CFrame
		LastHit = Train
		if Train ~= LastHit then
			LastTrainCFrame = Train.CFrame
		end
	else
		LastTrainCFrame = nil
	end
	
	if not Function2 then
		Function2 = player.Character.Humanoid.Died:Connect(function()
			Function:Disconnect()
			Function2:Disconnect()
		end)
	end
end)

How can I fix this? The ship uses bodymovers FYI.

1 Like

I recognize that code snippet from this thread which is used to keep players on moving platforms. This is meant to simulate physics when you’re moving the platforms without physics (ie tween/cframing). Since your ship uses body movers, that means it has physics and so the player should already be carried along with the movement of the ship. I imagine that plus the script you’re using to simulate physics is causing the player to slide forward.

Yes, without this script, the player gets carried along by default. But what I’m trying to fix is when the player jumps. When the player jumps, they don’t get carried along with the motion of the ship. This is important for players to be able to jump around on ships so they can access turrets or the captain’s quarters.

The script makes jumping on a ship possible, but then it also makes the player slide forward when standing still. This is what I’m trying to fix.

Well obviously the use of both of these things is creating a problem. Unless you want to create a hacky fix that has the script only run when the player is jumping, it would be best to just move the ship some other way without physics.