Attempt To Nil Index With Character Error In Script Within A Model

Hello Devs, I Have A Script That Checks If The Player is on A Part Using Raycasting But The Script keeps giving me the error Workspace.MovableItems.MoveIt.Script:3: attempt to index nil with 'Character'
I have checked serval different topics But Not Much has worked/Changed Anything Anyways here is the script

local player = players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()

local RunService = game:GetService('RunService')

local LastTrainCFrame

local Function
local Function2

Function = RunService.Heartbeat:Connect(function()

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

	local RootPart = character.LowerTorso

	local Ignore = character

	local ray =,,-50,0))

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

	if Hit and Hit.Name == "MoveIt" then

		--------------------------------------------------------------- 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.
		local TrainCF = Train.CFrame 

		local Rel = TrainCF * LastTrainCFrame:inverse()

		LastTrainCFrame = Train.CFrame -- Updated here.

		RootPart.CFrame = Rel * RootPart.CFrame -- Set the player's CFrame

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


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


If anyone knows what is happening that would be greatly appreciated as I’m Not A “Pro” scripter. Thanks For Reading

try adding a waitforchild for this
in between player and character
also this is in a local script, right?


Thanks The Wait For Child Worked After I Turned The Script Into A Local Script And inserted it Into StarterCharacterScripts Thanks For The Help!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.