My Data store script won't save unless edit at data store editor

Hi, i writing stats system for my game but there is problem about save function, it doesn’t save for me
it will become same stats like in data store editor

Sorry for bad grammar

local DataStoreService = game:GetService("DataStoreService")
local datakey = "SilverAru"
local datastore = DataStoreService:GetDataStore(datakey)
local player = game:GetService("Players")
function SaveData(player)
	if player.UserId < 0 then return end
	player:WaitForChild("Stats")
	
	local data = {}
	for i, v in pairs(player.Stats:GetChildren()) do
		table.insert(data,{v.Name, v.Value})
	end
	local sucess, errorMessage = pcall(function()
		datastore:SetAsync(player.UserId, data)
	end)
	if not sucess then
		local t = tick()
		repeat wait(0.1)
			sucess = pcall(function()
				datastore:SetAsync(player.UserId,data)
			end)
		until sucess or tick()-t > 15
	end
	
	print(data)
end

game.Players.PlayerRemoving:Connect(SaveData)

This is my save script there is load script but i copy a few script on my script

It’s probably not saving because you are in Studio. In Studio, PlayerRemoving doesn’t always fire, so we use game:BindToClose to cover for it.

local DataStoreService = game:GetService("DataStoreService")
local datakey = "SilverAru"
local datastore = DataStoreService:GetDataStore(datakey)
local player = game:GetService("Players")
function SaveData(player)
	if player.UserId < 0 then return end
	player:WaitForChild("Stats")
	
	local data = {}
	for i, v in pairs(player.Stats:GetChildren()) do
		table.insert(data,{v.Name, v.Value})
	end
	local sucess, errorMessage = pcall(function()
		datastore:SetAsync(player.UserId, data)
	end)
	if not sucess then
		local t = tick()
		repeat wait(0.1)
			sucess = pcall(function()
				datastore:SetAsync(player.UserId,data)
			end)
		until sucess or tick()-t > 15
	end
	
	print(data)
end

game.Players.PlayerRemoving:Connect(SaveData)

game:BindToClose(function()
	for i,v in pairs(game.Players:GetPlayers()) do
		SaveData(v)
	end
end)
2 Likes

Hi, PlayerRemoving is fire correctly it print data table
But SaveData Function Won’t SetAsync lastest stats but it saved old stats
And i also turned studio api on

I personally don’t understand why most people make a separate function and then call it at the specific event. I suggest you make it like this:


local DataStoreService = game:GetService("DataStoreService")
local datakey = "SilverAru"
local datastore = DataStoreService:GetDataStore(datakey)


game:GetService('Players').PlayerRemoving:Connect(function(player)

if player.UserId < 0 then return end
	player:WaitForChild("Stats")
	
	local data = {}
	for i, v in pairs(player.Stats:GetChildren()) do
		table.insert(data,{v.Name, v.Value})
	end
	local sucess, errorMessage = pcall(function()
		datastore:SetAsync(player.UserId, data)
	end)
	if not sucess then
		local t = tick()
		repeat wait(0.1)
			sucess = pcall(function()
				datastore:SetAsync(player.UserId,data)
			end)
		until sucess or tick()-t > 15
	end
	
	print(data)
end)

Not sure if I made any mistake as I am on mobile right now, but yoy can try it at least in another script.

1 Like