Datastore won't Save

whoops sorry, here’s another video : 2023 03 10 18 38 10 - YouTube

You can still use the remote event, but the one you are going to check has to be the server side

Keep in mind: Thats just checking if an item changed, but with extra steps, which isnt nessacary for a DataStore to work, you can just have a value with a changed event on the Serber to only detect server changes, the Client doesnt need to play a role in it.

Ok, I get it, sorry if I said something wrong.

Try using this script.

local DataStoreService = game:GetService("DataStoreService")
local WinsData = DataStoreService:GetDataStore("WinsData")

game.Players.PlayerAdded:Connect(function(Plrs)
	if not Plrs:FindFirstChild("leaderstats") then
		local Folder1 = Instance.new("Folder", Plrs)
		Folder1.Name = "leaderstats"
	end
	
	local Folder1 = Plrs.leaderstats
	
	local Wins = Instance.new("IntValue", Folder1)
	local Coins = Instance.new("IntValue", Folder1)

	Wins.Name = "Wins"
	Coins.Name = "Coins"
	
	local GetSaved
	local Success = pcall(function()
		GetSaved = WinsData:GetAsync("Id_"..Plrs.userId)
	end)
	if GetSaved then
		Wins.Value = GetSaved[1]
		Coins.Value = GetSaved[2]
	
	else
		Wins.Value = 0
		Coins.Value = 0		
	end
end)

game.Players.PlayerRemoving:Connect(function(Plrs)
	WinsData:SetAsync("Id_"..Plrs.userId, {
		Plrs.leaderstats.Wins.Value, 
		Plrs.leaderstats.Coins.Value
	}) 
end)

If you don’t understand the script, I’ll explain it to you, I hope it works.

Unfortunately this didn’t work also

Try this,if it won’t work then can you say are scripts to earn wins and coins are local or this script is local?

local DataStoreService = game:GetService("DataStoreService")
local PlayersService = game:GetService("Players")

local DataStore = DataStoreService:GetDataStore("DataStore")

PlayersService.PlayerAdded:Connect(function(Player)
	
	Player:WaitForChild("leaderstats")
	local leaderstats = Player:FindFirstChild("leaderstats")
	
	leaderstats:WaitForChild("Wins")
	local Wins = leaderstats:FindFirstChild("Wins")
	
	leaderstats:WaitForChild("Coins")
	local Coins = leaderstats:FindFirstChild("Coins")
	
	local PlayerUserId = "Player_"..Player.UserId
	print(PlayerUserId)
	
	--Load Data--
	
	local Data
	local success, erromessage = pcall(function()
		Data = DataStore:GetAsync(PlayerUserId)
	end)
	
	if success == true then
		if Data then
		Wins.Value = Data.Wins
			Coins.Value = Data.Coins
			end
	end
		
end)

PlayersService.PlayerRemoving:Connect(function(Player)
	local PlayerUserId = "Player_"..Player.UserId
	
	local Data = {
		Wins = Player.leaderstats.Wins.Value;
		Coins = Player.leaderstats.Coins.Value
	}
	local success, errormessage = pcall(function()
	DataStore:SetAsync(PlayerUserId, Data)
	end)
	if success then
		print("Data successfully saved!")
	else
		print("There was an error!")
		warn(errormessage)
	end
end)

Hello! It looks like the code you provided is attempting to save player data when a player is removed from the game. However, you mentioned that the code fails without any error messages after printing “Starting Save”.

One possible reason for this issue could be that the WinsData variable is not properly defined or initialized. Make sure that you have defined WinsData as a valid DataStoreService using the following code:

local DataStoreService = game:GetService("DataStoreService")
local WinsData = DataStoreService:GetDataStore("WinsData")

Also, make sure that your game has permission to access the DataStoreService. You can check this by going to the game’s settings page and navigating to the “Security” tab, then ensuring that the “DataStores” permission is enabled.

If you have already checked these things and the issue persists, try adding more print statements to your code to help you debug the issue. For example, you can add a print statement after each SetAsync call to see which one is causing the issue.

Yeah, he realized that but it still didn’t work, check that here.

1 Like

I edited it, sorry for error when i posted it.

This script is completely broken. Checking for leader stats before it even exists will result in an infinite yield

I’ve tried everything you said but nothing works

Based on the code you provided, it seems like you are using the pcall function to catch any errors that occur when saving the player data. However, you are not checking the errormessage variable to see what the error is. It’s possible that there is an issue with the WinsData object, or there could be a problem with the player’s leaderstats object.

Here are a few suggestions for troubleshooting:

  1. Check that the WinsData object exists and has been properly initialized. You can do this by using the DataService:GetDataStore method to retrieve the data store and making sure that it is not nil:
local DataService = game:GetService("DataStoreService")
local WinsData = DataService:GetDataStore("WinsData")

if not WinsData then
    error("Failed to get WinsData store")
end
  1. Verify that the player’s leaderstats object exists and has the expected values. You can do this by printing out the values before trying to save them:
print("Wins:", player.leaderstats.Wins.Value)
print("Coins:", player.leaderstats.Coins.Value)
  1. Check the output window to see if any error messages are being printed. If there are errors, they may provide more information about what is going wrong.

If none of these steps resolve the issue, you can try adding more print statements throughout the code to see where the script is failing. For example, you could add a print statement after each SetAsync call to check whether it was successful:

local success = WinsData:SetAsync(player.UserId.."-wins", player.leaderstats.Wins.Value)
if success then
    print("Wins saved successfully")
else
    print("Failed to save wins")
end

By isolating the problem and adding more print statements, you should be able to figure out what is going wrong with your data saving system.

As repeated many times on this thread, I have used print statements and it stops functioning up to the point it should be saving data. The datastore IS properly clarified as it loads data just fine. I don’t wether or not you actually read my original code, but it very clearly checks for errors after the call.

No, it work, i tested it. And idk what are you talking about.

Oh i know now, i added leaderstats in another script.You can add here if you want.

I don’t know why it works for everyone else, but doesn’t work for me. Im starting to think its a problem with Roblox then with my code

Maybe it is local script?Try to reinstall studio if anything don’t work.

This is a really long thread for no reason with many solutions, report to #bug-reports if you actually think it’s a bug, otherwise make a new place.