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

How to solve this error message I get trying to teleport character?

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

5 answers

✓ Accepted answer

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)

4 comments

0

What are the hashtags on game for?

0

#game.Players:GetPlayers() gets the total number of players in the server.

0

I Wrote what you did and It says Character is a nil value

0

Oops, I did ".Caracter.:", it should be ".Character:". Sorry. Also, this is because the character wasn't added yet. I edited my post, please check.

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

1 comment

0

I agree with you on the game.Workspace thing, but the rest are obsolete, because; On the first post you understand he wants between 2 and 4, and here you used above or equal to 2. This would mean 5 and higher would work, which he doesn't want. Plus, using "if torso then" is useless because you used WaitForChild which will not run the next code until character was found.



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

1 comment

0

Please format your code correctly.

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.

6 comments

0

what I was trying to do was when that many players are online they all teleport to a location

0

The read my bio thing is really annoying to see. Please stop posting that

0

Well then get people to indent properly!

1

yeah, stop bragging about that sore bio

0

then what? should i just copy and paste what my bio says into part of my answer then?

0

loser you are always talking crap about my posts, good job you got -2 votes

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