Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

Attempted to index a nil value?

Asked by 6 years ago

I get the following error when I try to update the display that tells the player how much money they have:

119:15:32.798 - ServerScriptService.DataManager:47: attempt to index field '?' (a nil value)
219:15:32.798 - Stack Begin
319:15:32.799 - Script 'ServerScriptService.DataManager', Line 47
419:15:32.800 - Stack End

Script inside the serverscriptservice:

01local DS = game:GetService("DataStoreService")
02local Players = game:GetService("Players")
03local Run = game:GetService("RunService")
04 
05local playerDataStore = DS:GetDataStore("PlayerData")
06 
07local PlayerSesStats = {} -----Player data on current session
08local LoadedPlayers = {} -----Players whose data has already been loaded
09 
10local UpdEve = game.ReplicatedStorage.UpdateCash
11 
12local function DefaultStats()
13    return {
14        Money = 100
15    }
View all 76 lines...

The script that updates the player's currency display:

1local Display = script.Parent.MainGui.CashBox.Cash
2local UpdEve = game.ReplicatedStorage.UpdateCash
3 
4UpdEve.OnClientEvent:Connect(function(amount)
5    Display.Text = amount
6end)

What is causing the error and how do I fix it? And by the way, am I doing datascripts right so far?

0
Guests don’t exist anymore idk why you coded for guests User#19524 175 — 6y

1 answer

Log in to vote
1
Answered by
Avigant 2374 Moderation Voter Community Moderator
6 years ago

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.

0
yeah but it errors when i test it in studio and im not a guest radusavin366 617 — 6y
1
Right, because there is no loaded data the first time. Avigant 2374 — 6y
0
I don't seem to find a problem? At line 40 it loads the array from line 13 as PlayerSesStats[userid]??? Can you explain more in depth? radusavin366 617 — 6y
1
Yes, but that code is in this if statement "if loadSuccess and LoadedData then". Avigant 2374 — 6y
0
Oh, got it! thanks. radusavin366 617 — 6y
Ad

Answer this question