Problems with storing Data

Recently I’ve been working on a datastore for a game, but whenever I leave the game, data fails to save and I get no error. I’m not sure what I’ve been doing wrong despite looking over it multiple times.

API services are enabled and I’ve tried using BindToClose and PlayerRemoving just to get problems with both of them.

local playerId = "Player_"..player.UserId

		--// LOADING DATA
		print("LoadingData...")
		local success, errormessage = pcall(function()
			datasave = DataStore:GetAsync(playerId, datasave)
		end)	

		if success then 
			print("Loaded data!")
			player.Data.Alive.Value = datasave.alive
			player.Data.SwordEXP.Value = datasave.swordEXP
			
		end
game.Players.PlayerRemoving:connect(function(player)
	local playerId = "Player_"..player.UserId
	print(playerId)

	local datasave = {
		swordEXP = player.Data.SwordEXP.Value;
		igname = player.Data.IGName.Value;
		race = player.Data.Race.Value;
		alive = player.Data.Alive.Value;
		fistexp = player.Data.FistEXP.Value;
	}

	local success, errormessage = pcall(function()
		DataStore:SetAsync(playerId, datasave)
	end)

	if success then
		print("Data saved")
	else
		print("Data did not save")
	end

end)

Are you getting an error when you try to save?

Thanks to HugeCoolboy2007 for telling me it was a dictionnary i didn’t see it
So here’s the new code:

	--// LOADING DATA
	print("LoadingData...")
	local success, errormessage = pcall(function()
		datasave = DataStore:GetAsync(playerId, datasave)
	end)	

	if success then 
		print("Loaded data!")
		player.Data.Alive.Value = datasave['Alive']
		player.Data.SwordEXP.Value = datasave['swordEXP']
		
	end
local playerId = "Player_"..player.UserId

		--// LOADING DATA
		print("LoadingData...")
		local success, errormessage = pcall(function()
			return DataStore:GetAsync(playerId) -- just use the key, it doesn't take a second argument
		end)	

		if success then 
            if errormessage then -- "errormessage" would be whatever is returned, if an error occurs "success" would be false and everything in it's scope won't run
                -- this is also to check if the player has data
			    print("Loaded data!")
			    player.Data.Alive.Value = errormessage.alive
			    player.Data.SwordEXP.Value = errormessage.swordEXP
            else
               print("player had no data to load")
			end
        else
           warn("unable to get data:", errormessage) -- tells you the issue
		end

maybe this is the issue, are you running this in studio?

This only works for arrays, they’re storing a dictionary

I was running it in studio, but I tried switching to that code and it always gives me the MSG that data didn’t save, I’ll try running the script ingame

DataStores don’t work correctly in studio, only in a live game

It wasn’t working ingame either, at the top of the script I make it randomize new data if the alive value == false, could that be why?

can I see the changes in the new script

local playerId = "Player_"..player.UserId

		--// LOADING DATA
		print("LoadingData...")
		local success, errormessage = pcall(function()
			return DataStore:GetAsync(playerId) -- just use the key, it doesn't take a second argument
		end)	

		if success then 
			if errormessage then -- "errormessage" would be whatever is returned, if an error occurs "success" would be false and everything in it's scope won't run
				-- this is also to check if the player has data
				print("Loaded data!")
				player.Data.Alive.Value = errormessage.alive
				player.Data.SwordEXP.Value = errormessage.swordEXP
			else
				print("player had no data to load")
			end
		else
			warn("unable to get data:", errormessage) -- tells you the issue
		end
game.Players.PlayerRemoving:connect(function(player)
	local playerId = "Player_"..player.UserId
	print(playerId)

	local datasave = {
		swordEXP = player.Data.SwordEXP.Value;
		igname = player.Data.IGName.Value;
		race = player.Data.Race.Value;
		alive = player.Data.Alive.Value;
		fistexp = player.Data.FistEXP.Value;
	}

	local success, errormessage = pcall(function()
		DataStore:SetAsync(playerId)
	end)

	if success then
		print("Data saved")
	else
		print("Data did not save")
	end

end)