Help with CFrame

You can write your topic however you want, but you need to answer these questions:
Hello there!
I am currently making a script, when you have a item in your inventory and click a part you get teleported to a other part.

For some reason it says "Workspace.Part.Script4: attempt to index nil with “HumanoidRootPart” and it is not working!


image

I have looked on the devforum already but I am not able to find any solutions for the issue.
It would be great if someone could help me!
Thanks for reading.
-Maxi

script.Parent.ClickDetector.MouseClick:Connect(function(plr)
	if plr.Backpack["Room 101"]
	then
		plr.Character.HumanoidRootPart.CFrame = game.Workspace.Roomteleportblocks.Room101tp.CFrame

	end
	
end)
1 Like

So the player’s character seems to be nil for some reason and I can’t see why. Have you tried adding some prints to see line by line what’s happening?

I didn’t yet, I’ll do that right now.
So:

script.Parent.ClickDetector.MouseClick:Connect(function(plr)
	if plr.Backpack["Room 101"]
	then
print("owns room")

		plr.Character.HumanoidRootPart.CFrame = game.Workspace.Roomteleportblocks.Room101tp.CFrame
print("teleport worked?")

	end
	
end)

I tried but it doesn’t puts anything in the developer console.

You should use plr.Backpack:FindFirstChild("Room 101") instead of plr.Backpack["Room 101"] because otherwise it will error if the tool does not exist inside backpack.

Alright, thank you!
I tried that and it still says the same:

Use extra WaitForChild(“Room 101”) if you are sure it will exist

It’s inside a ClickDetector anyways so that’s not necessary plus that would cause bugs and that’s not what the OP is trying to do here.

If you are in the studio or “Output” in “View”, that will be easier to observe.

I can’t do it in studio, as the system which gives you the tool only works in roblox and not in studio.

You can also add a check to see if humanoidrootpart exist:

if plr.Character and plr.Character:FindFirstChild("HumanoidRootPart") and plr.Backpack:FindFİrstChild("Room 101") then
    print("owns room")
	plr.Character.HumanoidRootPart.CFrame = game.Workspace.Roomteleportblocks.Room101tp.CFrame
    print("teleport worked?")
end

Or simply use pcall ()

pcall(function()
   plr.Character.HumanoidRootPart.CFrame = game.Workspace.Roomteleportblocks.Room101tp.CFrame
)

I actually never used pcall, I’ll take a look how it works. Thanks for all the replies!

You shouldn’t really use pcalls or xpcalls unless you are sending datastore or sending HTTP requests or using loadstring() in your code where you don’t know if the code that will be put on there will be correct.

They will run slower than a normal call and isn’t necessary for most of the time.

1 Like

Try adding print(“test”, plr, plr.Character)

Unfornatuely it doesn’t prints anything.

Where did you put that print in the script? It should at least print “test”

Try printing all of the player variables first to see if they are valid? print("plr") print("plr.Name") print("plr.Character.HumanoidRootPart")

It didn’t print anything.

script.Parent.ClickDetector.MouseClick:Connect(function(plr)
	if plr.Character and plr.Character:FindFirstChild("HumanoidRootPart") and plr.Backpack:FindFirstChild("Room 101") then
		print(plr) print(plr.Name) print(plr.Character.HumanoidRootPart)
		plr.Character.HumanoidRootPart.CFrame = game.Workspace.Roomteleportblocks.Room101tp.CFrame
		print("teleport worked?")
	end
end)

I meant put it before the if statement, that way you can see what could be wrong with your code

1 Like