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 :/
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.
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!