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!
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)
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)
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.
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
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.
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)