Issue changing values in a table using module

Hi! I’m lately trying to make a module, that can can store/change values.

I’m having that problem when I’m trying to change any kind of value in a specific table, that I created for the player, it always errors.

I’m trying to achieve that when the module gets required by a script, then by the given arguments it should change the value in the given player’s table.

I tried searching on the devforum for help, but I didn’t find any topics that can help my problem.

this is how my module looks like:

(Please don’t mind the debug sections)

local dataManager = {}

-- dataTable{ player = { ingame = false   }   }

dataManager.dataTable = {}
dataManager.debugMode = true

dataManager.createTable = function(plr)
	
	--[[ argument checks ]]--
	
	if typeof(plr) ~= "string" then return warn("Player argument isn't a string!") end
	
	--[[ create table ]]--S
	
	dataManager.dataTable[plr] = {inGame = false,inShop = false}
	
	--[[ debug ]]--
	
	if dataManager.debugMode then
		for i, v in pairs(dataManager.dataTable) do
			print(tostring{i}.." / "..tostring(v))
		end
	end
end

dataManager.setTable = function(plr,toChange,toValue)
	
	--[[ argument checks ]]--
	
	if typeof(plr) ~= "string" then return warn("Player argument isn't a string!") end
	if typeof(toChange) ~= "string" then return warn("ToChange argument isn't a string!") end
	
	--[[ more data & table ]]--
	
	--// getting the player table
	
	local mainTable = dataManager.dataTable
	
        -- this is where I'm having problems with the script

	local playerTable = mainTable[plr] or warn("No?!?!?!?!")
	local playerValue = playerTable[toChange] or warn("Nope didn't work") -- always shows the warn
	
	--// changing the value
	
	--playerTable = toValue    -- removed this for now, because if I would keep it it would always say
        --the value I had given the module, while it will stay say the warning message
	
	--// debug
	
	if dataManager.debugMode then
		print(playerValue)     -- always returns nil
		warn(plr)
		warn(toChange)
		warn(toValue)
	end
end

dataManager.debugTable = function()
	if dataManager.debugMode then
		for i, v in pairs(dataManager.dataTable) do
			for o,b in pairs(v) do
				print(tostring{o}.." / "..tostring(b))
			end
			
			warn(tostring{i}.." / "..tostring(v))
		end
	end
end

return dataManager

Here are the two functions:

-- used to create a table in the main dataTable

local module = require(game.ReplicatedStorage.dataManager) 
module.createTable("player")

-- used to change a value in the given player's table
local module = require(game.ReplicatedStorage.dataManager) module.setTable("player","inGame",true)
dataManager.setTable = function(plr,toChange,toValue)
 --[[ argument checks ]]--
if typeof(plr) ~= "string" then return warn("Player argument isn't a string!") end
if typeof(toChange) ~= "string" then return warn("ToChange argument isn't a string!") end

--[[ more data & table ]]--

--// getting the player table

local mainTable = dataManager.dataTable

local playerTable = mainTable[plr] or warn("Player table doesnt exist")

-- Make sure the player table is existing first
if playerTable then
    -- Setting the value
    dataManager.dataTable[plr][toChange] = toValue
    -- Now we check the value
    local playerValue = dataManager.dataTable[plr][toChange]
    
    if dataManager.debugMode then
        print(playerValue)
        warn(plr)
        warn(toChange)
        warn(toValue)
    end
   end
 end
1 Like