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

Can someone help me with the error, "Attempt to index nil with CFrame?"

Hello, I am very new to scripting. I've been watching AlvinBlox's YoutTube series on how to make a game as a starting point in my Lua Learning. The problem lies within trying to teleport players to a specific part on a cloned map. I'll include the teleport portion of the main script, and the video that I'm currently learning from. I've been comparing my script with AlvinBlox's, but I cannot find the solution.

The map is successfully cloned when I test it, but the problem lies within, character:FindFirstChild("HumanoidRootPart").CFrame = AvailableSpawnPoints[1].CFrame which leads to the teleport script not being able to function, the error being "attempt to index nil with CFrame"

Thanks in advance.

local SpawnPoint = ClonedMap:FindFirstChild("SpawnPoint")
	
	if not SpawnPoint then
		print("Spawnpoints not found")
	end
	
	local AvailableSpawnPoints = SpawnPoint:GetChildren()
	
	for i, player in pairs(plrs) do
		if player then
			character = player.Character
			
			if character then
				-- Teleport
				
				character:FindFirstChild("HumanoidRootPart").CFrame = AvailableSpawnPoints[1].CFrame
				table.remove(AvailableSpawnPoints,1)
				
				--Give sword
				
				local Sword = ServerStorage.Sword:Clone()
				Sword.Parent = player.Backpack
				
				local GameTag = Instance.new("BoolValue")
				GameTag.Name = "GameTag"
				GameTag.Parent = player.Character
				
			else
				-- There is no character
				if not player then
					table.remove(plrs,i)
				end
			end
		end
	end

1 answer

Try to make:

character = player.Character or player.CharacterAdded:Wait()
wait()

Or problem can be in HumanoidRootPart:

HMP = character:WaitForChild("HumanoidRootPart")
wait()
HMP.CFrame = AvailableSpawnPoints[1].CFrame

2 comments

0

Thanks for the solution, but I'm still presented with the same error with the CFrame. This is what I did: I added "or player.CharacterAdded:Wait()" and "wait()" to the already existing "character = player.Character" I already assumed that I had to define "HMP", so I wrote "local HMP = character:WaitForChild("HumanoidRootPart")" and followed it with the rest of the code you suggested.

0

I played around with the string value for HMP, adding such things as "Head" after WaitForChild, but got the same error.

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