My datastore isn't working (again)

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    Hi again, I had a solution from the last post I made, but that’s not working either. I want my leaderstats to save.

  2. What is the issue? Include screenshots / videos if possible!
    When I leave and rejoin, my leaderstats save, but on alt accounts, it doesn’t work and stats get reset. No error messages.

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I have tried debugging for hours. It’s really urgent as I just launched a game.

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

--Script by Deadlox85854868.


local datastoreservice = game:GetService('DataStoreService')
local datastore = datastoreservice:GetDataStore("myDataStore")

local function playerAdded(player)
	wait(.5)
	local character = player.Character or player.CharacterAdded:Wait()
	local Data = datastore:GetAsync(player.UserId)
	if Data then
		player.leaderstats["Spent ($)"].Value = Data["Spent"]
		player.leaderstats["Earned ($)"].Value = Data["Earned"]
		player.leaderstats["Played"].Value = Data["Played"]
		player.leaderstats["Time Played"].Value = Data["TimePlayed"]
	end
end

local function playerRemoved(player)
	local character = player.Character or player.CharacterAdded:Wait()
	local success, errorMsg = pcall(function()
		datastore:UpdateAsync(player.UserId, function()
			return {
				
				Spent = player.leaderstats["Spent ($)"].Value,
				Earned = player.leaderstats["Earned ($)"].Value,
				Played = player.leaderstats["Played"].Value,
				TimePlayed = player.leaderstats["Time Played"].Value,
			}
		end)
	end)
	if errorMsg then
		print(errorMsg)
	end
end

game.Players.PlayerAdded:Connect(playerAdded)
game.Players.PlayerRemoving:Connect(playerRemoved)

game:BindToClose(function()
	wait(3)
end)

Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.

Is your Access to API Services enabled?
Also try to put the data you’re saving inside of a table and save that table instead:

local newData = {
				Spent = player.leaderstats["Spent ($)"].Value,
				Earned = player.leaderstats["Earned ($)"].Value,
				Played = player.leaderstats["Played"].Value,
				TimePlayed = player.leaderstats["Time Played"].Value,
}

If that doesn’t work neither, try using my script I have on my profile, it automatically saves data without requiring any code. I’ll try to link it here.

https://devforum.roblox.com/t/i-made-a-saving-stats-script-that-is-fully-configurable-and-supports-multiple-stats/1939951

What do alt accounts have to do with this? I don’t see anything that allows you to load the data on an account with a different userID.

A broken datastore script might be able to handle the owner’s data, but not new players because it errors when making new data.

@DevExperiment2021 The root table Data was checked, but you actually didn’t check if the indexes of the Data are present or not.

You should add criteria for the indexes.

if Data ~= nil and Data["Spent"] and Data["Earned"] and Data["Played"] and Data["TimePlayed"] then
	player.leaderstats["Spent ($)"].Value = Data["Spent"]
	player.leaderstats["Earned ($)"].Value = Data["Earned"]
	player.leaderstats["Played"].Value = Data["Played"]
	player.leaderstats["Time Played"].Value = Data["TimePlayed"]
end

With any datastore problems, I recommend looking into the modules Datastore2 or ProfileService because it does the work for you. But, if you really want to learn datastores, go for it.

What data leakage are you talking about? Datastore2 works fine, I’ve been using it for a long time.

I’m not the original poster,

Oops, sorry lol. I meant that datastore2 was made because datastores used to have a data leakage problem. Its a fine tool, but I dont think its very useful anymore.

I think it was made to circumvent the datastore limits so there’s absolutely no data loss. Even if it doesn’t provide any benefit to your own datastore system, I’d still use it because I don’t want to make my own system!

1 Like