HI;

I can't seem to figure out why this code is causing an error. I use the same format in a seperate function and it works just fine.

function funMod.TeleportAllPlayersToLobby(lobbySpawnPoint)
	print(_G.PlayerData().GetPlayersInGame())
	for _, player in pairs(_G.PlayerData().GetPlayersInGame()) do
		if player.PlayingGame.Value == true then
			player.PlayingGame.Value = false
		end
		player.HumanoidRootPart.CFrame = lobbySpawnPoint.CFrame + Vector3.new(0,5,0)
	end
	
	for _, player in pairs(_G.PlayerData().GetPlayersSpectating())do
		if player.PlayingGame.Value == true then
			player.PlayingGame.Value = false
		end
		player.HumanoidRootPart.CFrame = lobbySpawnPoint.CFrame + Vector3.new(0,5,0)
	end
	funGUI.DevLog("# of players is " .. #_G.PlayerData().GetPlayersInLobby())
end

function funMod.TeleportAllPlayersToGame(num)
	for _, player in pairs(_G.PlayerData.GetPlayersInLobby()) do
		if (player) then
			--check to see if they are taking a break
			if player.Choices.SkipPlaying.Value ~= true then
				funMod.TeleportSinglePlayerToRoom(player, num)
				funMod.SetupPlayerCameraOnRoom(num)
				_G.PlayerData.RemovePlayerFromLobby(player)
				_G.PlayerData.RemovePlayerFromSpectators(player)
				_G.PlayerData.AddPlayerToGame(player)
			end
		else
			print"no player associated with character reference"
		end
	end
end

Line 02 exists only because the error was happening on 03 causes the error when I attempt to access a table stored as a global variable given by a function _G.PlayerData().GetPlayersInGame()

The error does not occur while using the same premise on Line 20, _G.PlayerData.GetPlayersInLobby() This code works just fine.

The function is accessing a variable in a PlayerData Server script which is accessing the following modulescript:

local funUtils = require(game.ServerScriptService.FunctionScripts.funUtils)

local funPlayerData = {}

local tblPlayersInLobby = {}
local tblPlayersInGame = {}
local tblPlayersSpectating = {}

-- -----------------------------------------------------------------

function funPlayerData.AddPlayerToLobby(player)
    table.insert(tblPlayersInLobby, #tblPlayersInLobby+1, player)
end

function funPlayerData.RemovePlayerFromLobby(player)
    funUtils.RemoveFromTable(tblPlayersInLobby, player)
end

function funPlayerData.GetPlayersInLobby()
	return tblPlayersInLobby
end

-- ------------------------------------------------------------

function funPlayerData.AddPlayerToGame(player)
    table.insert(tblPlayersInGame, #tblPlayersInGame+1, player)
end

function funPlayerData.RemovePlayerFromGame(player)
    funUtils.RemoveFromTable(tblPlayersInGame, player)
end

function funPlayerData.GetPlayersInGame()
	return tblPlayersInGame
end

-- ------------------------------------------------------------

function funPlayerData.AddPlayerToSpectators(player)
    table.insert(tblPlayersSpectating, #tblPlayersSpectating+1, player)
end

function funPlayerData.RemovePlayerFromSpectators(player)
    funUtils.RemoveFromTable(tblPlayersSpectating, player)
end

function funPlayerData.GetPlayersSpectating()
	return tblPlayersSpectating
end

-- ------------------------------------------------------------

function funPlayerData.RemovePlayerFromAll(player)
    funPlayerData.RemovePlayerFromGame(player)
    funPlayerData.RemovePlayerFromLobby(player)
    funPlayerData.RemovePlayerFromSpectators(player)
end

return funPlayerData

Thanks you for taking the time to read this.

3 comments

0

I don't recommend using _G or shared and I'm also missing information on your post. What's the error? Where are you actually defining the global variables? Your error could be the cause of you defining the variable after you call it.

0

I couldn't see the forest for the trees, for hours. I've been calling _G.PlayerData() as a function, instead of a value. Lol, I wasn't doing that on line 20. I'm just using _G for small global server variables. There should be nothing wrong with it. The only missing from is a server script called player handler, it only references the Module Script Player data by instantiating it as _G.PlayerData

0

this is from the player manager script. ~~~~~~~~~~~~~~~~~ local funPlayerData = require(game.ServerScriptService.FunctionScripts.funPlayerData) local function GetPlayerData() return funPlayerData end _G.PlayerData = GetPlayerData() ~~~~~~~~~~~~~~~~~ Thanks for trying to help, sorry it was just a typo my brain wasn't seeing this whole time.