That sounds pretty good. I’ll try.
local pointsName = "Points"
local DataStore = game:GetService("DataStoreService"):GetDataStore("PointsStore_db1")
local Cache = {}
local Connections = {}
local function SaveInfo(Player, Value)
local ID = pointsName .. "-" .. player.UserId
DataStore:SetAsync(ID, Value)
print(player.Name .. "'s points were saved.")
end
game.Players.PlayerAdded:Connect(function(player)
local folder = Instance.new("Folder")
folder.Name = "leaderstats"
folder.Parent = player
local points = Instance.new("IntValue")
points.Name = pointsName
points.Parent = folder
Connections[Player] = points:GetPropertyChangedSignal("Value"):Connect(function()
Cache[Player] = points.Value
end)
spawn(function()
while wait(AppropriateTime) and Player do
SaveInfo(player, points.Value)
end
end)
local ID = pointsName.."-"..player.UserId
local Success, Data, Error = pcall(function()
return DataStore:GetAsync(ID)
end)
if Success and Data then
points.Value = Data
print("Player currency data loaded." .. " | JNS |")
else
print("New player added to the currency system" .. " | JNS |")
end
end)
game.Players.PlayerRemoving:Connect(function(player)
if Cache[player] then
SaveInfo(player, Cache[Player])
end
if Connections[player] then
Connections[player]:Disconnect()
end
end)
Just change the portion I have named AppropriateTime to a decent auto save time, and it should work hand and hand with the other final statement to more benefit you when it comes to data saving.
Also noticed that I tend to use capital P for Player when you used p for player so uh, might need to watch out for those.
Thank you very much for the code and your help, it really is a huge help! I was looking in the Developer Wiki more about how to save user data using tables.
I meant when you set the points from the console - are you setting them on the client or server?
I call an event which simulates the purchase of the car, which includes changing the value of the player’s points.
One more thing I’d like to mention, if you’re testing in studio, the studio server tends to like to shut down before data can be stored properly unless you have a game:BindToClose(function() wait(5) end), but I wouldn’t recommend doing this.
The same thing happens again, only the first points that I add after starting with 0 are saved.
Just testing in Roblox Player, I set my points to 10, bought a car, which subtracted me 5 points, and when I disconnected and reconnected, the points returned to 10.
I used something similar, check my code out
local DSService = game:GetService("DataStoreService")
local DataSave = DSService:GetDataStore("Gamedata")
game.Players.PlayerAdded:connect(function(plr)
local leader = Instance.new("Folder",plr)
leader.Name = "Status"
local money = Instance.new("IntValue",leader)
money.Name = "Money"
money.Value = 5600
local uniquekey = "id-"..plr.UserId
repeat
wait(.1)
until DSService:GetRequestBudgetForRequestType(Enum.DataStoreRequestType.GetAsync) > 4
local isSaved = DataSave:GetAsync(uniquekey)
if isSaved then
if isSaved[1] and isSaved[1] > 1 then
money.Value = isSaved[1]
end
else
repeat
wait(.1)
until DSService:GetRequestBudgetForRequestType(Enum.DataStoreRequestType.SetIncrementAsync) > 2
DataSave:SetAsync(uniquekey,{money.Value})
end
game.Players.PlayerRemoving:connect(function(plrLeave)
if plrLeave.UserId == plr.UserId then
local t = 0
repeat
t = t + 1
local success, failmsg = pcall(function()
DataSave:UpdateAsync(uniquekey,function(oldvalue)
return {money.Value}
end)
end)
wait(3)
if not success then
print("error: "..t.." tries, message: "..failmsg)
end
until success or t > 3
end
end)
As you can see, I used tries, so if there’s more than 3 tries, the ode will stop and print the fail message.
Everything seems to be working correctly with the tries. The points are saved. Thank you!