Coins returned as nil

I’m attempting to print out the amount of coins the player has as soon as they join. However, it keeps returning as nil. I’ve tried all I can think of, but nothing has worked. Code snippets and error below.

Module Script:

function data.LoadData(player)
	local Datastore = DataStore2("PlayerData2", player)
	local PlayerData = Datastore:Get(Default)

	data.Coins = 1000
	data.UnlockedZones = "Town"
-- Rest of the code

Server Script:

local data = require(game.ServerScriptService.DataManager)

game:GetService("Players").PlayerAdded:Connect(function(player)
	player.CharacterAdded:Wait()
	local pdata = data.LoadData(player)
	print(pdata.Coins)
end)

Error in Output:

ServerScriptService.Script:6: attempt to index nil with 'Coins'  -  Server - Script:6
Stack Begin  -  Studio
Script 'ServerScriptService.Script', Line 6  -  Studio - Script:6
Stack End  -  Studio

Any help is appreciated.

1 Like

What are you returning from data.LoadData? Also, you are storing the return value from DataStore:Get(Default) in a variable named PlayerData, but then modify data which seems incorrect.

1 Like

come again?
ill say what i think you mean, im trying to get the coin variable from the loaddata, not sure about the other thing

1 Like
local PlayerData = Datastore:Get(Default)

You created a local variable named PlayerData.

data.Coins = 1000
data.UnlockedZones = "Town"

But here you’re changing values on data and not PlayerData. Is that intentional?

Also, data.LoadData is returning nil which is why you’re seeing the error in the output. What are you returning in that function? It’s hard to know what is going wrong without knowing what you’re returning in that function.

1 Like

ooh that makes more sense, I’ll change the data to PlayerData. As for the returning, im assuming you mean what the function is itself? Sorry if i’mwrong about that, i’m not the best with English.
Heres the code for the function:

function data.LoadData(player)
	local Datastore = DataStore2("PlayerData2", player)
	local PlayerData = Datastore:Get(Default)

	data.Coins = 1000
	data.UnlockedZones = "Town"

	local function coinsUpdated(updatedVal)
		data.Coins = Datastore:Get(updatedVal)
	end

	local function zonesUpdated(updatedVal)
		data.UnlockedZones = Datastore:Get(updatedVal)
	end

	coinsUpdated(Default)
	Datastore:OnUpdate(coinsUpdated)

	zonesUpdated(Default)
	Datastore:OnUpdate(zonesUpdated)
-- havent touched anything below this, ignore whats below
	for i, v in pairs(PlayerData) do
		pcall(function()
			Config:SetAttribute(i, v)
			Config:GetAttributeChangedSignal(i):Connect(function()
				PlayerData[i] = Config:GetAttribute(i)
			end)
		end)
	end

	--Config.Parent = player
end```

Yea that’s what I meant.

So it looks like you’re not returning anything from that function so local pdata = data.LoadData(player) will always be nil. Try returning PlayerData to see if that fixes the issue.

sorry if im being annoying but, do i do this?

	local pdata = data.LoadData(player).PlayerData

So for a function to return a value, you use the return keyword. Consider a simple example:

local function Add(ValueA, ValueB)
   return ValueA + ValueB
end

local Sum = Add(10, 5)
print(Sum) -- prints '15'

You see how we use the return keyword to “return” a value to whatever is calling that function?. You would need to do that for your LoadData function: return PlayerData

Alright so i’ve messed with the code a bit and it ALMOST seems to work. I changed data.LoadData(player) to data.LoadData(player, PlayerData) and the server script to this:

local data = require(game.ServerScriptService.DataManager)

game:GetService("Players").PlayerAdded:Connect(function(player)
	player.CharacterAdded:Wait()
	local pdata = data.LoadData(player, PlayerData)
	print(pdata.Coins)
end)

Says ServerScriptService.Script:6: attempt to index number with 'Coins' in output however. Also thank you so much for your help

You’re probably returning the value and not the object, if that’s what your trying to get at

No problem :slight_smile: Also, you don’t need to include PlayerData in the data.LoadData function call.

That new error you’re seeing means that whatever you’re returning from data.LoadData is a number and not a table like you’re expecting. If you’re returning PlayerData in data.LoadData then you need to double check that the value you’re saving to the data store is indeed a table and not just a number.

1 Like

Got it working! Thanks for the help!

1 Like

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