Datastore keep resetting

  1. 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)

1 Like

You’re setting the data value to “balance.Value”, which causes your values reset to the default value, which is 0

Thanks so much, I took around an hour trying to figure out what to do :sweat_smile:

1 Like

But wait, if I don’t set it to “balance.Value” it would say I cant store an instance in a datastore

I’m assuming where this line of code is in is where your data should be loaded?

I posted the code, can you take a look at it and tell me if you see anything wrong?

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:

balance.Value = data[balance.Name]   

Another thing, in your data, you don’t think you’re supposed to reference to player’s name, the ID of the player should suffice.

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.

Save your player data with ProfileService! (DataStore Module) - Resources / Community Resources - DevForum | Roblox

This is a very simple issue which im quite surprised how you haven’t noticed it yet, if you script still doesn’t work then thats another issue

It should be balance.Value = data, because the data is the variable that has the balance value, simple.

1 Like

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.

2 Likes

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

2 Likes

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.

1 Like

Doesn’t save in studio. You have to do it in the Roblox client. Tiedious process.

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.

1 Like