A problem with DataStores

Hi guys, i have a problem with datastores. I made my dataStore for a value which i called progress. It worked and it saved the value, but after changing it - the old value is still showing. I have no idea what is causing this. I’m new at the dataStores so i have no idea what to do. Here is the script:

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

local database = DataStoreService:GetDataStore("data")
local sessionData = {}

function playerAdded(player)
	local Folder = Instance.new("Folder")
	Folder.Name = "ProgressFolder"
	Folder.Parent = player
	local progress = Instance.new("NumberValue")
	progress.Name = "Progress"
	progress.Parent = Folder
	progress.Value = 0
	
	local success = nil
	local plrData = nil
	local attempt = 1

	repeat
		success, plrData = pcall(function()
			return database:GetAsync(player.UserId)
		end)

		attempt+= 1
		if not success then
			warn(plrData)
			task.wait(3)
		end
	until success or attempt == 5	

	if success then
		print("Connected to data")
		if not plrData then
			print("Assigning deafult data")
			plrData = {
				["Progress"] = 0
			}
		end
		
		sessionData[player.UserId] = plrData
	else 
		warn("Unable to get data for ", player.Name)
		player:Kick("ERROR: " .. player.Name .. "_DATA UNABLE TO LOAD. TRY LATER")
	end
	
	progress.Value = sessionData[player.UserId].Progress
	
	progress.Changed:Connect(function()
		sessionData[player.UserId].Progress = progress.Value
	end)
end

Player.PlayerAdded:Connect(playerAdded)

function playerLeaving(player)
	if sessionData[player.UserId] then
		local success = nil
		local errorMsg = nil
		local attempt = 1
		
		repeat
			success, errorMsg = pcall(function()
				database:SetAsync(player.UserId, sessionData[player.UserId])
			end)
			
			attempt+= 1
			
			if not success then
				warn(errorMsg)
				task.wait(3)
			end
			
		until success or attempt == 5	
		
		if success then
			print("Saved data for", player.Name)
		else
			warn("unable to save for", player.Name)
		end
	end
end

Player.PlayerRemoving:Connect(playerLeaving)

Also if you need to ask me questions - go ahead, i will try to answer them.

1 Like

You never call game:BindToClose which could be the reason it’s failing (the server could be shutting down and disconnecting the players and in the process players.PlayerRemoving is not firing in time).

If this doesn’t solve it are you changing the value on the client (ie from a local script or in the command bar but not switching to server mode)? If so, it won’t replicate to the server so you need to be changing it on the server.

You could also take a different approach and just compile the data that needs to be saved from the folder you’re creating when the player leaves (which is what I’d do personally).

1 Like

The problem was that i changed the value on the client not the server. Thank you for help! Also i made bind to close in case of crashing but it didn’t work then. Ty again for help!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.