Jailbreak train script not working

earlier i found a script helping me with a current problem, although when entering and changing the script up it caused errors:

this error is spammed every 0.5 seconds even when not on the platform.

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

local LastTrainCFrame

local Function
local Function2


Function = RunService.Heartbeat:Connect(function()

--------------------------------------------------------------- CHECK PLATFORM BELOW

local RootPart = player.Character.LowerTorso

local Ignore = player.Character

local ray = Ray.new(RootPart.CFrame.p,Vector3.new(0,-50,0))

local Hit, Position, Normal, Material = workspace:FindPartOnRay(ray,Ignore)

if Hit and Hit.Name == "MovingPart" then -- Change "RaftTop" to whatever the moving part's name is

--------------------------------------------------------------- MOVE PLAYER TO NEW POSITON FROM OLD POSITION

local Train = Hit
if LastTrainCFrame == nil then -- If no LastTrainCFrame exists, make one!
	LastTrainCFrame = Train.CFrame -- This is updated later.
end
local TrainCF = Train.CFrame 

local Rel = TrainCF * LastTrainCFrame:inverse()

LastTrainCFrame = Train.CFrame -- Updated here.

RootPart.CFrame = Rel * RootPart.CFrame -- Set the player's CFrame
--print("set")

else
LastTrainCFrame = nil -- Clear the value when the player gets off.

end

Function2 = player.Character.Humanoid.Died:Connect(function()
	Function:Disconnect() -- Stop memory leaks
	Function2:Disconnect() -- Stop memory leaks
end)

end)

You could do local Ignore = player.Character or player:CharacterAdded:Wait() if that doesn’t work, you can just keep what you have defining character, but before that line do repeat wait() until player.Character, but it is not really advised.

but either way, the script doesnt even work. i stand on the so called MovingPlatform and slide off.

The error is saying that the player doesn’t exist. LocalPlayer only exists in LocalScripts. Make sure your script is a LocalScript and not a Script.

Make sure the script is a local script
And put it in startergui
And the platform has to move using cframe

1: why startergui?
2: i use this script:

local Platform = workspace.Platform – Your platform
shared.TweenService = game:GetService’TweenService’
local StartingPosition, EndingPosition = Vector3.new(0,0,0), Vector3.new(10,10,10) – where to start and end
Platform.Position = StartingPosition
shared.TweenService:Create(Platform,
TweenInfo.new(5, Enum.EasingStyle.Quart, Enum.EasingDirection.InOut, -1, true), {
Position = EndingPosition
}
):Play() – This will go back and forth every 5 seconds.

why startergui?

StarterGui/StarterPlayer are both good options, the issue with StarterCharacter/StarterPack is the fact that they will both clear and “regenerate” each time the player is killed. This can cause some interesting and unforeseen artifacts that wouldn’t be an issue otherwise.

Overall, it saves unnecessary coding to prevent the aforementioned artifacts form occuring in the first place.

So, 1: I just place the part in starter gui? Or the script
2: is that script I send using CFrame