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

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

Asked by 4 years ago
Edited 4 years ago
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

Log in to vote
2
Answered by
Velsity 218 Moderation Voter
4 years ago

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.

0
Oh, how do I save some data into it? zeSauce 0 — 4y
0
SetAsync. Like you did in playerremoving. But since you first start with a nil data value, theres no data. So it errors. Velsity 218 — 4y
Ad
Log in to vote
0
Answered by
Sulu710 142
4 years ago

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!

0
Theres no need for brackets. You can access the table both ways. Velsity 218 — 4y
0
Wait for real? Sulu710 142 — 4y

Answer this question