So I want to make a game like Dead Rails. I currently am having an issue where when the train moves, the player doesn’t move with it while they’re on it, the floor just slides away beneath them. If you’ve ever played Dead Rails, you’ll know that your character moves with the train, and you can also walk and jump while you’re on the train.
How do I achieve this kind of behavior? Here’s the script I’m using btw:
(It’s a Script
(NOT LocalScript
) placed inside ServerScriptService
.
local RunService = game:GetService("RunService")
local part = workspace:FindFirstChild("Part")
wait(5)
if part and part:FindFirstChild("Attachment") then
local attachment = part.Attachment
local speed = 1
local bobIntensity = 3
local bobSpeed = 2
local timeElapsed = 0
RunService.Heartbeat:Connect(function(deltaTime)
timeElapsed = timeElapsed + deltaTime
local targetPosition = attachment.WorldPosition
local bobOffset = Vector3.new(0, math.sin(timeElapsed * bobSpeed) * bobIntensity, 0)
part.Position = part.Position:Lerp(targetPosition + bobOffset, speed * deltaTime)
end)
end
And here’s how the workspace and explorer should look like:
Note that the object that is selected in the viewport is the attachment and the part is, well the part. The attachment is INFRONT of the part (the blue circle is the orientation indicator of the part).
Also, the part is Anchored
.
Thank you!