I Want players in a table to teleport in a specific position. But, the output says that HumanoidRootPart Is Nil.
And I have made a table and saw,
[1] = Brioche_Noodle [2] = "*** cycle table reference detected ***"
Function That I am focusing on,
function spawnMap() local Maps = Storage.Maps:GetChildren() chosenMap = Maps[math.random(1, #Maps)] chosenMap:Clone() chosenMap.Parent = workspace.Map for _, player in ipairs(Playing) do if player then print(player) player.Character.HumanoidRootPart.Position = Vector3.new(chosenMap.Teleporter.Position) end end end
Here is my whole script.
local Players = game:GetService("Players") local Teams = game:GetService("Teams") local Storage = game:GetService("ServerStorage") local Playing = {} local NotPlaying = {} local Infected = {} local Team = { Lobby = Teams.Lobby, Infected = Teams.Infected, Citizens = Teams.Citizens } local gameStarted = false local chosenMap local function onPlayerAdded(player) print(player.Name .. " || Joined || ") if gameStarted == false then print(player.Name .. " is added to The Playing Table") table.insert(Playing, player) print(Playing) else print(player.Name .. " is added to The Not Playing Table") table.insert(NotPlaying, player) print(NotPlaying) end player.Team = Team.Lobby end local function addPlayerToPlaying() local AllPlayers = Players:GetPlayers() table.insert(Playing, Playing) end local function addPlayerToNotPlaying() local AllPlayers = Players:GetPlayers() table.insert(NotPlaying, Playing) end function spawnMap() local Maps = Storage.Maps:GetChildren() chosenMap = Maps[math.random(1, #Maps)] chosenMap:Clone() chosenMap.Parent = workspace.Map for _, player in ipairs(Playing) do if player then print(player) player.Character.HumanoidRootPart.Position = Vector3.new(chosenMap.Teleporter.Position) end end end function despawnMap() local Maps = Storage.Maps:GetChildren() chosenMap.Parent = nil for _, player in ipairs(Playing) do if player then player.CharacterAdded:Connect(function(Character) Character.HumanoidRootPart.Position = Vector3.new(chosenMap.Lobby.Teleporter.Position) end) end end end -- Listeners Players.PlayerAdded:Connect(onPlayerAdded) -- Script while true do gameStarted = false wait(4) gameStarted = true addPlayerToPlaying() spawnMap() wait(10) despawnMap() Playing = nil NotPlaying = nil Infected = nil end
Change your spawnMap()
function to the following:
function spawnMap() local Maps = Storage.Maps:GetChildren() chosenMap = Maps[math.random(1, #Maps)] chosenMap:Clone() chosenMap.Parent = workspace.Map for _, player in ipairs(Playing) do if player then print(player.Name.." is in Playing table") local char = player.Character or player.CharacterAdded:Wait() if char then print(player.Name.."'s character exists") player.Character.HumanoidRootPart.Position = Vector3.new(chosenMap.Teleporter.Position) end end end end
I tested the script in my own place, so this should work. If you still get the same (or a different) error, get back to me and I'll see if I can help more.
You need to check the player's character still exists as well as the player