Is this Auto-Save safely made?

Hello. Is this a right way to do it? This is my own autosave


while true do

local players = game.Players:GetChildren()

for i, player in pairs(players) do
	
	local Data = game.ServerStorage.Data:FindFirstChild(player.Name)
	
	local success, err = pcall(function()
			
		if Data then
		
			local coins = Data.Coins.Value
			local equipped = Data.Equipped.Value
			local stage = Data.Stage.Value
			local speed =  Data.Gamepasses.Speed.Value
			local NoCooldown =  Data.Gamepasses.NoCooldown.Value
			local VIP =  Data.Gamepasses.VIP.Value
			local x2Coins =  Data.Gamepasses.x2Coins.Value
			local piggykills = player.leaderstats.Kills.Value
			local TotalCoins = player.leaderstats.TotalCoins.Value
			----------------------------------------------------
			
			local success, err = pcall(function()
				
				myDataStore:SetAsync(player.UserId.."_Coins",coins)
				
			end)
			if success then print(player.Name..": "..coins.." coins saved") end
			wait(0.3)
			----------------------------------------------------
			
			local success, err = pcall(function()
				
			myDataStore:SetAsync(player.UserId.."_Equipped",equipped)
				
			end)
			if success then print(player.Name..": "..equipped.." equipped saved") end
			wait(0.3)
			----------------------------------------------------
			
			local success, err = pcall(function()
				
				myDataStore:SetAsync(player.UserId.."_Stage",stage)
				
			end)
			if success then print(player.Name..": "..stage.." stage saved") end
			wait(0.3)
			----------------------------------------------------
			
			local success, err = pcall(function()
				
				myDataStore:SetAsync(player.UserId.."_Speed",speed)
				
			end)
			if success then print(player.Name..": "..speed.." speed gamepass saved") end
			wait(0.3)
			----------------------------------------------------
			
			local success, err = pcall(function()
					
				myDataStore:SetAsync(player.UserId.."_NoCooldown",NoCooldown)
				
			end)
			if success then print(player.Name..": "..NoCooldown.." NoCooldown gamepass saved") end
			wait(0.3)
			----------------------------------------------------
			
			local success, err = pcall(function()
				
				myDataStore:SetAsync(player.UserId.."_VIP",VIP)
				
			end)
			if success then print(player.Name..": "..VIP.." vip gamepass saved") end
			wait(0.3)
			----------------------------------------------------
			
			local success, err = pcall(function()
				
				myDataStore:SetAsync(player.UserId.."_x2Coins",x2Coins)
				
			end)
			if success then print(player.Name..": "..x2Coins.." x2coins gamepass saved") end
			wait(0.3)
			----------------------------------------------------
			----------------------------------------------------------
			local tries = 0	
			repeat	wait(0.4)
			tries = tries + 1
			local success, err = pcall(function()
				
				myDataStore:SetAsync(player.UserId.."_TotalCoins",TotalCoins)
				
			end)
			if not success then 
				warn(err)
			end
			until tries == 3 or success
			----------------------------------------------------------
			local tries = 0	
			repeat	wait(0.4)
				tries = tries + 1
				local success, err = pcall(function()
					
					myDataStore:SetAsync(player.UserId.."_PiggyKills",piggykills)
					
				end)
				if not success then 
					warn(err)
				end
			until tries == 3 or success
				
		end
		
	end)
	
	if not success then print("Error autoguardado para "..player.Name)	end
					
end

wait(50)

end

1 Like

Please copy your code into a ``` box. It makes it much easier for us to help you.

1 Like

Hi, excuse me! I’m noob at this, there you go

1 Like

Exploit wise this looks good. As long as you’re doing all the saving via the server you will not have any exploiters messing with your datastores.

Nice job!

1 Like

Hey there, your code is perfectly fine but there reptitive pcall and retries is bad.

You can instead try making a function which does the whole thing for you instead of copying and pasting again and again.

Example:

function safeSave(callback)

    local Retries = 0
    local Success, Error = pcall(callback)

    if not (Success) then
        repeat wait(.5)
            Success, Error = pcall(callback)
            Retries = Retries + 1
        until Retries >= 3 or Success
    end

end)

safeSave(function()
   myDataStore:SetAsync(player.UserId .. "_Speed", Speed)
end)
1 Like

Thank you man. I don’t know why I didn’t realize that