Datastore not saving

A few days ago, I made a working datastore. Everything saved perfectly, then today, when I made a new key, the key wouldn’t save. I’m not sure about the others.

Not sure if this relates but right before I made the new key, I downloaded a plugin called “Datastore Editor”.

local myDataStore = DataStoreService:GetDataStore("myDataStore")

game.Players.PlayerAdded:Connect(function(player)

	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "datastore_folder"
	leaderstats.Parent = player



	local Credits = Instance.new("IntValue")
	Credits.Name = "Credits"
	Credits.Parent = leaderstats

	local SP = Instance.new("IntValue")
	SP.Name = "SP"
	SP.Parent = leaderstats

	local Activity = Instance.new("IntValue")
	Activity.Name = "Activity"
	Activity.Parent = leaderstats
	
	local BanCheck = Instance.new("IntValue")
	BanCheck.Name = "BanCheck"
	BanCheck.Parent = leaderstats



	local playerUserId = "Player_"..player.UserId

	local data
	local success, errormessage = pcall(function()
		data = myDataStore:GetAsync(playerUserId)
	end)

	if success then
		if data then
			Credits.Value = data.Credits
			SP.Value = data.SP
			Activity.Value = data.Activity
			BanCheck.Value = data.Banned
		end
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	local playerUserId = "Player_"..player.UserId

	local data = {
		Credits = player.datastore_folder.Credits.Value;
		SP = player.datastore_folder.SP.Value;
		Activity = player.datastore_folder.Activity.Value;
		BanCheck = player.datastore_folder.BanCheck.Value;
	}

	local success, errormessage = pcall(function()
		myDataStore:SetAsync(playerUserId, data)
	end)

	if success then
		print("Data successfully saved")
	else
		print("There was an error saving data")
		warn(errormessage)
	end
end)

The key which is not saving is BanCheck.

Edit: Also, when I leave the game it prints “Data successfully saved” even though obviously it didn’t

1 Like

Are you sure the error isn’t on the data load?

I’m sorry I don’t understand. What do you mean?

did you enable API access on Roblox studio so it can actually save?

why did you name leaderstats to "datastore_folder
2 instead of ’ leaderstats’ will that work?

1 Like

Yes I did. Everything else is disabled.

I changed it to “datastore_folder” because if I choose leader stats then it’ll show everything in the playerlist.

if you dont want it to show try and make a custom player list so you can control what is shown and what isnt

True true. But even if I made it into leader stats, I don’t think it would still save. :thinking:

A new key is usually not the issue, to narrow down the issue I believe this plugin might relate to the cause.

Yeah, I’ll try uninstalling the plugin.

if data then
		Credits.Value = data.Credits
		SP.Value = data.SP
		Activity.Value = data.Activity
		BanCheck.Value = data.Banned
	end

shouldn’t this “BanCheck.Value = data.Banned” be BanCheck.Value = data.BanCheck

Wait, that might be the problem. I’ll try changing that.

Maybe something here is the issue, the if data then. Since you got a new key, you probably don’t have any data to begin with, so data is nil.

That also might be the problem.

1 Like

I put if data then because if you’re new, then it’ll make an error. But the next time it wouldn’t make an error. I usually wouldn’t mind but since it’s a simple if condition, I added that. I feel like if I look into the console and see 0 errors I feel satisfied lol.

Maybe if there’s no data, add default values instead.

How would I add default values?

You can add a line that states data = data or {Credits=0, SP=0, Activity=0, Banned=0} and remove the if data then statement.

I found the fix. :partying_face:

2 options were:

  1. Datastore not saving - #14 by SubtotalAnt8185
  2. Datastore not saving - #12 by LegendPX_Taken

Tho they didn’t fix the issue, they helped fix possible future bugs in my script and improved it so thanks!

SOLUTION:

In a different script, which I use to ban people, I realized that I kicked the player before changing the values, so the IntValue doesn’t exist.

How I realized this: I went into the console and entered game.Players.Simply_Developers.datastore_folder.BanCheck.Value = 1, so it’s straightforward.
(Simply_Developers is my alt since I can’t ban myself lol)

Thanks to everyone who tried to help me with this script!

1 Like