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

Why does this array read fine in one function, but empty in another?

I have tried reading the array variable Players by accessing it like module.Players from an outside script, and I have also had a getStat function which returned Players, however the getStat function (which was inside the module) read the array as empty, even though changeStat reads it as having one entry.

****statService (Module In ServerScriptService)

local module = {}
	module.Players = {}
	
	function checkPlayers(plr)
		for i = 1,#module.Players do
			if module.Players[i][1] == plr then
				return i
			end
		end
		return 0
	end


	module.changeStat = function(player,stat,value)
		local index = checkPlayers(player.Name)
		
		if index == 0 then

			module.Players[#module.Players+1] = {player.Name,0,0,0,0} --Player Name,K,D,Dm,H
			index = checkPlayers(player.Name)
		end
		
		module.Players[index][stat] = module.Players[index][stat] + value
		
		for a = 1,#module.Players do
			for b = 1,#module.Players[a] do
				print(b..": "..module.Players[a][b])
			end
		end
	end
	
return module

2 answers

?????? Just do

game.Players:GetPlayers()

..

1 comment

0

It's a place created from the create place api, so not all players will join the game exactly at the same time, so I made it that as soon as they have a stat to record, it will add them to the table. Unfortunately, that's not really an answer to my issue since it doesn't affect the reason why it is acting the way it is.

To any who have issues with Module Scripts and saving data, the issue was me using the command bar to test them. The command bar is at a more elevated environment which causes it's state to be essentially 'saved' when the game is played. This meant that even if I added an element to array, if I tried to access it using the command bar it wouldn't see it.

To counteract this issue, you need to test modules with scripts / local scripts.

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