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

Players will not move to part?

I have cloned a part to workspace inside a folder named mapHolder. Anyways there are spawns in there that I am trying to make every player teleport to, however, this will not work and there are no errors.

Teleport code:

-- this code should teleport a player to a random spawn part on the map
-- mapSelected is a string value which indeed holds the name of the map

function teleportPlayers()
	for i,v in pairs(allPlayers) do
		wait()
		if allPlayers[v] ~= nil then
			print("Player is there")
			if allPlayers[v].Character ~= nil then
				print("Character is there")
				if allPlayers[v]:FindFirstChild("Humanoid").Health ~= 0 then
					print("Player is alive")
					local mapSpawns = mapHolder[mapSelected.Value]:FindFirstChild("Spawns"):GetChildren()
					allPlayers[v].Character:FindFirstChild("HumanoidRootPart").CFrame = mapSpawns[math.random(1, #mapSpawns)].CFrame-- + Vector3.new(0, 3, 0)
				end
			end
		end
	end
end

1 answer

✓ Accepted answer

On this line,

 if allPlayers[v] ~= nil then

You are trying to do allPlayers[Player] instead of the index of the player in the table.

It should be either just v or allPlayers[i] like this,

function teleportPlayers()
	    for i,v in pairs(allPlayers) do
	        wait()
	        if v ~= nil then
	            print("Player is there")
	            if v.Character ~= nil then
	                print("Character is there")
	                if v:FindFirstChild("Humanoid").Health ~= 0 then
	                    print("Player is alive")
	                    local mapSpawns = mapHolder[mapSelected.Value]:FindFirstChild("Spawns"):GetChildren()
	                    v.Character:FindFirstChild("HumanoidRootPart").CFrame = mapSpawns[math.random(1, #mapSpawns)].CFrame-- + Vector3.new(0, 3, 0)
	                end
   	         end
	        end
	    end
	end

2 comments

0

Weird, it still doesn't work.

0

Fixed it.... allPlayers I guess was the problem instead I just did game.Players:GetChildren()

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