Data isn't nil, but game thinks it is?

I use this little part of the code to send the player’s money to a local script for it to show in a shop.
image
This code worked before but now it’s suddenly giving an error!


If anyone is wondering what sessionData is:
image
I even tried to set the data back to default thinking theres a problem with that but it still gave me an error.
The script that requires the value “Money” to work looks like this:

If anyone has an idea how to fix this I would gladly listen!

Please use the </> option in the text editor here to post scripts. Myself I will not touch a question made up of pictures.

I use this little part of the code to send the player’s money to a local script for it to show in a shop.

getdata.OnServerInvoke = function(player)
	return sessionData[player.UserId].Money
end

This code worked before but now it’s suddenly giving an error!

ServerScriptService.Leaderboard and data save:130: attempt to index nil with 'Money' - Server - Leaderboard and data save:130

If anyone is wondering what sessionData is:

if success then
		playerData = {                                  --set default values
			["Kills"] = 0,
			["Money"] = 100,
			["Level"] = 1,
			["XP"] = 0,
			["ReqXP"] = 15
		}
		print("Connected to database")                      --self explanitory
		if not playerData then                              --if nil
			print("Asigning default data")                  
			playerData = {                                  --set default values
				["Kills"] = 0,
				["Money"] = 100,
				["Level"] = 1,
				["XP"] = 0,
				["ReqXP"] = 15
			}
		end
		sessionData[player.UserId] = playerData             --set the sessionData to the default values if player has no data

I even tried to set the data back to default thinking theres a problem with that but it still gave me an error.
The script that requires the value “Money” to work looks like this:

local gui = script.Parent.Parent
local exit = gui.Shop.Container.ExitButton
local money = gui.Shop.Container.Currency
local itemsframe = gui.Shop.Container.ScrollingFrame
local rs = game:GetService("ReplicatedStorage")
local getdatafunc = rs:WaitForChild("GetData")    --get data from datastore with remote function

local playerdata = getdatafunc:InvokeServer()     --get the data

local player = game.Players.LocalPlayer
local leaderstats = player:WaitForChild("leaderstats")
local Currency = leaderstats:WaitForChild("Money")

money.Text = Currency.Value .."$"

function updatemoney()
	money.Text = Currency.Value .."$"
end



local function toggleshop()                     --close shop when Close is clicked
	gui.Shop.Container.Visible = not gui.Shop.Container.Visible
	if gui.Shop.Container.Visible then
		playerdata = getdatafunc:InvokeServer()
	end
end

Currency:GetPropertyChangedSignal("Value"):Connect(updatemoney)    --constantly run the shop script
exit.Activated:Connect(toggleshop)    --

If anyone has an idea how to fix this I would love to hear your suggestions!

The error says its on the line 130 of the sever scrip. But seems that you are debugging the local script where you expect to get value of Money from the server table.

I think the issue is not on that code you are showing

My bad with the wording. The script which does not work is the 1st one. I just showed the local script so you could have an idea of how it works as it might help some people.

2 Likes

I see, my mistake.
I would suggest debug that server script, check the output, this will surely guide you to the issue:

getdata.OnServerInvoke = function(player)
	warn(player)
	warn(player.UserId)
	warn(sessionData[player.UserId])
	warn(sessionData[player.UserId]["Money"])
	return sessionData[player.UserId].Money
end

Thanks! It seems sessionData is nil for some reason, I’ll check my code again most likely forgot something simple

1 Like

Good to hear you found the issue. Try to print the sessionData data:

getdata.OnServerInvoke = function(player)
	warn(sessionData)
	return sessionData[player.UserId]["Money"]
end

Maybe its only the entry of the player which is not there

It seems the code just DIDNT want to work. After I did a lot of warns everywhere to find out why sessionData is nil, it stopped being nil, probably needs a small wait somewhere. Thanks again.