You need to save when the player leaves the game too. I also added some things to the script that will help.
Try this:
local DataStoreService = game:GetService("DataStoreService")
local Players = game:GetService("Players")
local DataStore = DataStoreService:GetDataStore("NameHere")
local function Load(plr)
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
local SavedStats
local Key = "plr-"..plr.UserId
pcall(function()
SavedStats = DataStore:GetAsync(Key)
end)
if SavedStats then
points.Value = SavedStats[1]
coins.Values = SavedStats[2]
else
local Values = {
points.Value;
coins.Value
}
pcall(function()
DataStore:SetAsync(Key, Values)
end)
end
end
local function Save(plr)
local Key = "plr-"..plr.UserId
local Values = {
plr.leaderstats.Points.Value;
plr.leaderstats.Coins.Value
}
pcall(function()
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!`
9 comments
Line 22. You make a table with curly braces {} not brackets ()
Still doesnt work.
try dis: points.Value = savedValues[1] , coins.Value = savedValues[2]
That is what I wrote..
No commas... See before you judge
So? Did it work or what?
Hello??
I will try. But how will I write it?
I wrote it with commas, and on the same line. But I just got an error. Thanks for the help anyways! :)