What is the issue? Datastore keeps resetting every time I rejoin
So I tried to make a datastore by myself since I’ve been just following tutorials and when I tried to do it by myself the balance resets to 0 every time
local playerService = game:GetService("Players")
local dataStore = game:GetService("DataStoreService"):GetDataStore("AlphaData")
local function saveData(plr)
local success, errormessage = pcall(function()
dataStore:SetAsync(plr.UserId..plr.Name, plr.Data.Balance.Value)
end)
if success then
print("successfully saved "..plr.Name.."'s data.")
else
print("couldnt save player's data")
warn(errormessage)
end
end
playerService.PlayerAdded:Connect(function(plr)
local plrData = Instance.new("Folder", plr)
plrData.Name = "Data"
local balance = Instance.new("NumberValue", plrData)
balance.Name = "Balance"
local data
local success, errormessage = pcall(function()
data = dataStore:GetAsync(plr.UserId..plr.Name)
end)
if success then
data = balance.Value
else
warn(errormessage)
end
end)
playerService.PlayerRemoving:Connect(function(plr)
local success, errormessage = pcall(function()
saveData(plr)
end)
if not success then
warn(errormessage)
end
end)
game:BindToClose(function()
for _, v in pairs(game.Players:GetPlayers()) do
local success, errormessage = pcall(function()
saveData(v)
end)
if not success then
warn(errormessage)
end
end
end)
I did, hence why I am asking, but if I am assuming this is where your data will be loaded, you should assign data to value, not value to data. Your data retrieves the information needed to assign data to value. Try inserting this line and see if it works:
I know this isn’t going to solve your specific problem but use Profile Service. Profile Service is used in many front-page games, and it’ll stop data loss once and for all. Using the regular data store saving method works, but eventually when your game gets more traffic, data will start to not save properly. Well, you can put systems in place to make them keep their data, but it won’t save at times still.
I hate to break it to you, but recommending a beginner developer with a more advanced data storing system isn’t going to help him fix his simple data store system and is only going to overwhelm him.
Don’t use plr.Name to set data otherwise if they change their username then their data will be wiped. Plr.Id is unique to each user and will not change so just use that
Do you realize the outrage and struggle it is when you have over thousands of players losing data? Yeah, it’s not easy. The best thing you can do for beginner developer is introduce them to Profile Service. Seriously! My game went through that when it was at around 500 players, and thank God I fixed it before it went up to 2k players.
I also explicitly said what I was saying wasn’t going to fix his issue, so please read the message before replying next time.
He’s just a beginner developer, his game won’t even get that popular, so that is something that he SHOULD do later when his games start getting players.