ERROR: Attempt to index local 'savedValues' (a number value)?
DataStore = game:GetService("DataStoreService"):GetDataStore("PeasStore") game.Players.PlayerAdded:connect(function(player) local stats = Instance.new("IntValue", player) stats.Name = "leaderstats" local points = Instance.new("IntValue", stats) points.Name = "Points" local coins = Instance.new("IntValue", stats) coins.Name = "Coins" local key = ("player-" .. player.UserId) local savedValues = DataStore:GetAsync(key) if savedValues then points.Value = savedValues[1] coins.Value = savedValues[2] else local valuesToSave = {points.Value} DataStore:SetAsync(key, valuesToSave) end end)
Error at line 19
``PLEASE HELP ME!
You need to save when the player leaves the game too. I also added some things to the script that will help.
Try this:
-- Define variables local DataStoreService = game:GetService("DataStoreService") local Players = game:GetService("Players") -- You should put your data store as a separate variable then the service one local DataStore = DataStoreService:GetDataStore("NameHere") -- Loads the data back to the player local function Load(plr) -- Creates the stats local leaderstats = Instance.new("Folder") leaderstats.Name = "leaderstats" leaderstats.Parent = plr local points = Instance.new("IntValue") points.Name = "Points" points.Parent = leaderstats local coins = Instance.new("IntValue") coins.Name = "Coins" coins.Parent = leaderstats -- Don't give a value yet local SavedStats local Key = "plr-"..plr.UserId -- key pcall(function() -- Stops errors -- Data Stores can error so thats why I did this SavedStats = DataStore:GetAsync(Key) end) -- Gives the player the data if they have data else create a datastore for them if SavedStats then points.Value = SavedStats[1] coins.Values = SavedStats[2] else local Values = { points.Value; coins.Value } pcall(function() -- Stops errors DataStore:SetAsync(Key, Values) end) end end -- Saves on leaving local function Save(plr) local Key = "plr-"..plr.UserId local Values = { plr.leaderstats.Points.Value; plr.leaderstats.Coins.Value } pcall(function() -- Stops errors DataStore:SetAsync(Key, Values) end) end Players.PlayerAdded:Connect(Load) Players.PlayerRemoving:Connect(Save)
I know it may look intimidating since it has 63 lines but it's just an upgraded version to what you had. Also make sure you have datastore enabled for your place. If you don't you can enable it by going to your place and clicking on the tree dots. Next press configure this place. Click on Games. Then press the Allow this place to be updated using the Save Place API in your game.
checkbox.
Hope this helps!`