Why is my table returning nil (print)

Hello, i’m new to working with tables to store stats.
i understand the basics now.

But i don’t understand why my local script for updating text is returning nil?
i think it might be because the stats for players are server sided and the local script is accessing the module script locally?

Local script(the one i have trouble with):


local datamodule = require(game.ReplicatedStorage.datamodule)
local playerdata = datamodule.getdata()

print(playerdata[game.Players.LocalPlayer.UserId]["stats"].gold) -- problematic line

script.Parent.Text = playerdata[game.Players.LocalPlayer.UserId]["stats"].gold

module script :

local datahandler = {}

local template = {["stats"] = {
	gold = 10,
	items = {}
}}

local playerTable = {}

function datahandler.getdata()
	return playerTable
end

function datahandler.addplayer(player, data)
	if player:IsA("Player") then
		if not data then
			playerTable[player.UserId] = table.clone(template)
		else
			playerTable[player.UserId] = data
		end
	else
		warn("not a player")
	end
end

function datahandler.removeplayer(player)
	if player:IsA("Player") then
		playerTable[player.UserId] = nil
	end
end

return datahandler

serverscript for stats:



local datamodule = require(game.ReplicatedStorage.datamodule)

game.Players.PlayerAdded:Connect(function(player)
	datamodule.addplayer(player)
	
	local playerdata = datamodule.getdata()
	
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local gold = Instance.new("IntValue")
	gold.Name = "gold"
	gold.Value = playerdata[player.UserId]["stats"].gold
	gold.Parent = leaderstats
end)

local function updateleaderstats(player)
	for _, v in pairs(player.leaderstats:GetChildren()) do
		v.Value = datamodule.getdata()[player.UserId]["stats"][v.Name]
	end
end

game.Players.PlayerRemoving:Connect(function(player)
	datamodule.removeplayer(player)
end)

game.ReplicatedStorage.RemoteEvent.OnServerEvent:Connect(function(player)

	local playerdata = datamodule.getdata()
	playerdata[player.UserId]["stats"].gold += 1
	
	updateleaderstats(player)
end)

Please, i really need help with this. Thank you for any responses. :smiley:

Yep you are correct, it does not replicate you will need attributes or remote function to handle this problem.

That makes my code unnecessarily complicated, i thought of doing that, are there any alternatives?
I wanted to use tables and modules to make it more simple for me :ˇ(

I feel ya, roblox dev is complicated.

Unfortunately, it is necessary.

If you want to simplify it you can use someone elses module that does this hardwork for you.

Edit: wow the server script in the module has 1000 lines. You probably dont need all the functionality just parts of it if you want to do it on your own.

1 Like

wait why doesn’t

local playerdata = game.ReplicatedStorage.RemoteEvent:FireServer("getdata")

script.Parent.Text = playerdata
game.ReplicatedStorage.RemoteEvent.OnServerEvent:Connect(function(player, request)
	local playerdata = datamodule.getdata()
	
	if request == "getdata" then
		return playerdata[player.UserId]["stats"].gold
	end
end)

work?, am i missing something? maybe i’m too tired :frowning:

1 Like

You adding player’s “profile” on server.
While trying to get it from client. And its not replicating.

1 Like

You will need a remote function, and also some sleep, good luck.

3 Likes

that wouldn’t really matter i tried both without userId and with it.

Thanks a lot!
Everything works now :smiley:

-------------- message limit

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.