I get the following error when I try to update the display that tells the player how much money they have:
1 | 19 : 15 : 32.798 - ServerScriptService.DataManager: 47 : attempt to index field '?' (a nil value) |
2 | 19 : 15 : 32.798 - Stack Begin |
3 | 19 : 15 : 32.799 - Script 'ServerScriptService.DataManager' , Line 47 |
4 | 19 : 15 : 32.800 - Stack End |
Script inside the serverscriptservice:
01 | local DS = game:GetService( "DataStoreService" ) |
02 | local Players = game:GetService( "Players" ) |
03 | local Run = game:GetService( "RunService" ) |
04 |
05 | local playerDataStore = DS:GetDataStore( "PlayerData" ) |
06 |
07 | local PlayerSesStats = { } -----Player data on current session |
08 | local LoadedPlayers = { } -----Players whose data has already been loaded |
09 |
10 | local UpdEve = game.ReplicatedStorage.UpdateCash |
11 |
12 | local function DefaultStats() |
13 | return { |
14 | Money = 100 |
15 | } |
The script that updates the player's currency display:
1 | local Display = script.Parent.MainGui.CashBox.Cash |
2 | local UpdEve = game.ReplicatedStorage.UpdateCash |
3 |
4 | UpdEve.OnClientEvent:Connect( function (amount) |
5 | Display.Text = amount |
6 | end ) |
What is causing the error and how do I fix it? And by the way, am I doing datascripts right so far?
The problem is that PlayerSesStats[player.UserId]
is nil
, and you are attempting to index the Money
key in it.
If the player is a guest, or if the DataStore
request failed, or if there was no data to be returned, the player's data in PlayerSesStats
will never be created in the first place, so attempting to index it will error.