I'm getting 'attempt to index nil with 'CFrame' error though the script work why?

My teleportation script works fine but I’m getting an error in output every time. What does Workspace.Teleports.Crane2Upper.Script:4: attempt to index nil with ‘CFrame’ mean?

I know my script works fine but how do I remove this error?

local TP = game.Workspace.Teleports.Crane2Lower
script.Parent.Touched:Connect(function(part)
	local hum = part.Parent:FindFirstChild("HumanoidRootPart")
	hum.CFrame = TP.CFrame + Vector3.new(0,5,0)
end)

Is this the entire script? TP isn’t defined.

Are you sure your block isnt touching another object in the workspace that is triggering the touched function such as the baseplate. To get around this just check if your hum variables actually exits by doing:

script.Parent.Touched:Connect(function(part)
	local hum = part.Parent:FindFirstChild("HumanoidRootPart")

    if hum then
	    hum.CFrame = TP.CFrame + Vector3.new(0,5,0)
    end
end)

bruh ok then

Here. I thought I pasted the whole script. TP is defined in the first line.

local TP = game.Workspace.Teleports.Crane2Lower
script.Parent.Touched:Connect(function(part)
	local hum = part.Parent:FindFirstChild("HumanoidRootPart")
	hum.CFrame = TP.CFrame + Vector3.new(0,5,0)
end)
local part = script.Parent
local TPS = workspace:WaitForChild("Teleports")
local TP = TPS:WaitForChild("Crane2Lower")

part.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("HumanoidRootPart") then
		local hum = hit.Parent:FindFirstChild("HumanoidRootPart")
		hum.CFrame = TP.CFrame + Vector3.new(0,5,0)
	end
end)
2 Likes

Thanks, I’ll test it and see if it works.

It works with no errors now, thank you!