Attempt to index nil with CFrame

Hey there, i want to spawn a wall in front of the player when this function is called, but im getting the error:
attempt to index nil with CFrame - line 25

What should i change to make the script work?

The localscript is in the StarterPlayerScripts

local function StopPlayer()

	local direction = rootPart.CFrame.LookVector

	local wallPosition = rootPart.Position + direction

	local wall = Instance.new("Part")
	wall.Size = Vector3.new(4, 8, 0.5)
	wall.CFrame = CFrame.new(wallPosition)
	wall.Parent = game.Workspace
end

Put the wall.CFrame line under workspace. Sometimes it can be the order of the code being a trouble maker

Small problem: I can’t see which line is 25. :smiley:

Is rootPart nil? If it is, then that is the problem.

local direction = rootPart.CFrame

That is indeed line 25, but rootPart isnt nil, because i can acccess it in other parts of the script.

Where and how do you define rootPart?

local player = game.Players.LocalPlayer
local Character = player.Character
local rootPart = Character:FindFirstChild("HumanoidRootPart")

Use :WaitForChild() and see if that works. Also, move this to StarterCharacterScripts.

1 Like