Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Can Someone Can Help me for this error attempt to index nil with 'CFrame'?



local Car2 = game.Workspace.Car2 -- Change "Car1" to the name of your car
local Seats = game.Workspace.Car2.Seats:GetChildren() -- Locates the Group of seats in your car
local Gui = script.Parent:FindFirstChild("ExitButton") -- Locates the Exit Button in the Teleport Part
local Gui2 = script.Parent:FindFirstChild("LoadingScreen") -- Locates the Exit Button in the Teleport Part

script.Parent.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		local player = game.Players:GetPlayerFromCharacter(hit.Parent)
			for i,v in pairs(Seats) do
				if Seats.Occupant == nil then
				hit.Parent.Humanoid.JumpPower = 0
				hit.Parent.Humanoid.WalkSpeed = 0
				Gui:Clone().Parent = player.PlayerGui
				player.Character.HumanoidRootPart.CFrame = Seats[math.random(1,17)].CFrame -- Teleports the player to a vacant seat
				wait(10)
				player.PlayerGui.ExitButton:Destroy() -- Change ExitGui to the name of your Exit Button Gui
				for i = 1,100 do -- Moves the car forward
					Car2:TranslateBy(Vector3.new(0,0,-1))
					wait() -- Do not change this line
				end
                Gui2:Clone().Parent = player.PlayerGui
                player.Character.Humanoid.JumpPower = 50
                wait(0.2)
                player.Character.Humanoid.Jump = true
                wait(0.2)
                player.Character.Humanoid.JumpPower = 0
                wait(0.2)
				wait()
				player.Character.HumanoidRootPart.CFrame = game.Workspace.GameTeleporter.CFrame -- Teleports the player to the Game Teleport brick
				wait(1)
				player.Character.Humanoid.JumpPower = 50
                wait(0.2)
                player.Character.Humanoid.Jump = true
                wait(0.2)
                player.Character.Humanoid.JumpPower = 0
				wait(15)
				for i = 1,100 do -- Moves Car Reverse
					Car2:TranslateBy(Vector3.new(0,0,1)) -- You might have to move the 1 in the position of a 0
					wait() -- Do not change this line
				end
			end
		end
	end
end)

1 answer

I have answered this so many times you won't believe, here is one of them: Attempt to index nil with bla bla bla

Or if you don't want to click the link then here are the solutions:

  1. Use :WaitForChild() instead of . and :FindFirstChild() when defining, it waits for the instance to load. It might be possible that the instance you tried to define did not load before the script ran.

  2. Check if the object exists. What if it is named for example "CaR" but you named it "caR" all capital letters must be the same. Also check if you defined all locations right.

The first solution should be working for you tho.

This discussion is preserved as it was. New answers, comments, and votes are closed.