I am trying to create a script for when A player joins they teleport to a random brick. When I click play to test I get this messageWorkspace.Teleport To Map:6: attempt to index field 'HumanoidRootPart' (a nil value)
I don't know what to do and would like some help. Thank you!
Here's My Code
game.Players.PlayerAdded:Connect(function(plyr) if game.Players.PlayerAdded == 2 or 3 or 4 then local Pos1 = game.Workspace.Spawn.Pos1 local Pos2 = game.Workspace.Spawn.Pos2 wait(2) game.Players.Name.HumanoidRootPart.CFrame = CFrame.new((Vector3.new(math.random(1,2)))) end end)
I don't understand what is wrong
Try this... feel free to accept answer The reason you are getting the error is because you said game.Players.name Another reason is because if game.Players.PlayerAdded... <-- you cannot do that plyr is the player... try this..
And I do not know what you mean by Spawn.Pos1
if something is odd, just see the raw code for this...
local teleportOne = workspace.Spawn.Pos1 local teleportTwo = workspace.Spawn.Pos2 game.Players.PlayerAdded:connect(function(NewPlayer) if NewPlayer then print("New dude has loaded") if NewPlayer.Character then print("New dudes character has loaded") if NewPlayer.Character.HumanoidRootPart then print("New dudes soul has loaded, teleporting him...") NewPlayer.HumanoidRootPart.Position = math.random(teleportOne, teleportTwo) end else print("Oh dang, so close") end else print("Destroiiiied") end else print("Wasted") end)
This is a double check if the player is there then it will teleport the player to either Pos1 or Pos2
The problem here is that you clearly forgot to use .Character
.
.Character
is very important when using the Player
to get the character, so don't forget to use it.
Also, I'd recommend you to use :WaitForChild
, because you are trying to access it as soon as player joins, but that might error if the loading takes a bit more.
You've got some fails in your coding too, and I'll fix them for you.
game.Players.PlayerAdded:Connect(function(plyr) if #game.Players:GetPlayers() == 2 or #game.Players:GetPlayers() == 3 or #game.Players:GetPlayers() == 4 then local Pos1 = game.Workspace.Spawn.Pos1 local Pos2 = game.Workspace.Spawn.Pos2 local pos = { Pos1.CFrame, Pos2.CFrame } wait(2) game.Players.Name.CharacterAdded:connect(function(c) c:WaitForChild('HumanoidRootPart').CFrame = pos[math.random(1, #pos)] end) end end)
First of all,
If you were trying to access the player,
you would have to do plyr.Character
not game.Players.Name.Character
By the way, the code I am revising is the answer from luadortog's answer
game.Players.PlayerAdded:Connect(function(plyr) if #game.Players:GetPlayers() >= 2 then -- Simplified the code a bit local Pos1 = workspace.Spawn.Pos1 -- Simplified game.Workspace to workspace(Its a shortcut) local Pos2 = workspace.Spawn.Pos2 local pos = {Pos1.CFrame, Pos2.CFrame} wait(2) local torso = plyr.Character:WaitForChild("HumanoidRootPart") if torso then -- If torso exists at all torso.CFrame = CFrame.new(pos[math.random(1, #pos)]) end end end)
If this fixed your code, or made others understand why you did wrong, please accept this answer, if not, please ask as many questions as you want
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~`~~~~~~~
place = CFrame.new(0,0,0) -- Where you want the player to go on touched script.Parent.Touched:connect(function(p)--Creating the function local humanoid = p.Parent:findFirstChild("Humanoid")--Trying to fi nd the humanoid if (humanoid ~= nil) then -- Checking if what touched it IS a player. humanoid.Torso.CFrame = place -- Moves the torso to what is specified in the place variable end end)--Ending the fuction, if you see on the function line above there is an unclosed parenthesis that I am closing here. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Just put in the pos of the block that you wna tpeople to teleport to then when you tuch it it will teleport you! If you dote no how to get the pos go in a block over wee you want to teleport them and go into prupterys then serch for pos Copy the numbers then past it into the brakkits...
The problem is, that HumanoidRootPart is not a valid member of a string, like you cannot do
print(game.Name.HumanoidRootPart)
Also, READ MY BIO. I AM NOT GONNA FIX YOUR INDENTING FOR YOU. So, you should do
game.Players.PlayerAdded:Connect(function(plyr) local numplayers = #game.Players:GetPlayers() if numplayers > 1 then local Pos1 = game.Workspace.Spawn.Pos1 local Pos2 = game.Workspace.Spawn.Pos2 wait(2) repeat wait until plyr.Character plyr.Character:WaitForChild('HumanoidRootPart').CFrame = game.Workspace.Spawn:GetChildren()[math.random(1, 2)] end end)
Also, what are you trying to do when u do if game.Players.PlayerAdded == 2 or 3 or 4
? PlayerAdded is a event, not a property.