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
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.
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.
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```
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.
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
No problem 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.