Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

"ServerScriptService.Leaderstats:10: attempt to index local 'plrData' (a nil value)"How to fix this?

local DataStore = game:GetService("DataStoreService")
local PointsData = DataStore:GetOrderedDataStore("SugarStore1")

game.Players.PlayerAdded:Connect(function(plr)
    local plrData = PointsData:GetAsync(plr.UserId)
    local leaderstats = Instance.new("Folder")
    leaderstats.Name = "leaderstats"
    
    local pointsValue = Instance.new("IntValue",leaderstats)
    pointsValue.Value = plrData.sugar
    leaderstats.Parent = plr
    pointsValue.Name = "Sugar"

    local pointsValue2 = Instance.new("IntValue",leaderstats)
    pointsValue2.Value = plrData.cash
    leaderstats.Parent = plr
    pointsValue2.Name = "Cash"
end)

game.Players.PlayerRemoving:Connect(function(plr)
    plrdata = {sugar = plr.leaderstats.Sugar.Value, cash = plr.leaderstats.Cash.Value}
    PointsData:SetAsync(plr.UserId, plrdata)    
    print("Data Saved")
end)

That's my code, I actually started scripting recently, so I do not really understand what the problem is :/

2 answers

It means that theres no saved data in there, thus it is nil. You should always check if there is data in that datastore first.

2 comments

0

Oh, how do I save some data into it?

0

SetAsync. Like you did in playerremoving. But since you first start with a nil data value, theres no data. So it errors.

The problem is in your table. When you create your table, in order to specify a certain index, you must put the index inside square brackets []. Also, "sugar" and "cash" are a stringvalue, so they need to have quotes ""around them. So when creating your table in the Player Removing part of your script, it should be:

plrdata = {["sugar"] = plr.leaderstats.Sugar.Value, ["cash"] = plr.leaderstats.Cash.Value}

Then when calling upon a certain value in your table, you use:

print(Table[Index])

So when a player is added, your script should be:

game.Players.PlayerAdded:Connect(function(plr)
    local plrData = PointsData:GetAsync(plr.UserId)
    local leaderstats = Instance.new("Folder")
    leaderstats.Name = "leaderstats"
 
    local pointsValue = Instance.new("IntValue",leaderstats)
    pointsValue.Value = plrData["sugar"]  -- Table[Index]
    leaderstats.Parent = plr
    pointsValue.Name = "Sugar"
 
    local pointsValue2 = Instance.new("IntValue",leaderstats)
    pointsValue2.Value = plrData["cash"]  -- Table[Index]
    leaderstats.Parent = plr
    pointsValue2.Name = "Cash"
end)

Also, I'm not sure if this would even make a difference, but I wouldn't use numbers in your variable names, so maybe instead of pointsValue2 you could use pointsValueTwo.

I hope all this works for you, good luck with your game!

2 comments

0

Theres no need for brackets. You can access the table both ways.

0

Wait for real?

This discussion is preserved as it was. New answers, comments, and votes are closed.