Repeat loop won't repeat, SetAsync not saving

  1. What do you want to achieve?

Save Player Cash Value on Exit.

  1. What is the issue? Include screenshots / videos if possible!

Followed Gnome Code’s tutorial word by word, but it seems I’ve encountered an error that he didn’t describe in the video. It seems that the function for PlayerRemoving works fine but the repeat loop isn’t running and the SetAsync isn’t telling me if the data was a success or not.

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

Made sure my code is exactly as written in the GnomeCode tutorial.

Here is the script

local Players = game:GetService("Players")
local DSS = game:GetService("DataStoreService")
local DataStore = DSS:GetDataStore("Money")
local sessionData = {}

function PlayerAdded(Player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	

	local cash = Instance.new("IntValue")
	cash.Name = "Cash"
	cash.Parent = leaderstats

	local success = nil
	local playerData = nil
	local attempt = 1

	repeat
		success, playerData = pcall(function()
			return DataStore:GetAsync(Player.UserId)
		end)

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

	if success then
		if not playerData then
			print("Assigning default data")
			end
			playerData = {
				["Cash"] = 50, 
			}
		end
		sessionData[Player.UserId] = playerData
		print("Connected Data",sessionData[Player.UserId])
	else
		warn("Unable to get data for", Player.Name)
		Player:Kick("Unable to load data. Try again later.")
	end

	cash.Value = sessionData[Player.UserId].Cash
	cash.Changed:Connect(function()
		sessionData[Player.UserId].Cash = cash.Value
	end)
	leaderstats.Parent = Player
end


function PlayerRemoving(Player)
	print("Player Leaving")
	if sessionData[Player.UserId] then
		local success = nil
		local errorMsg = nil
		local attempt = 1
		print("Session Data Found")
		repeat
			success, errorMsg = pcall(function()
				print("pcall running")
				DataStore:SetAsync(Player.UserId, sessionData[Player.UserId])
			end)
			print("repeating")
			attempt += 1
			
			if not success then
				warn(errorMsg)
				task.wait(1)
			end
		until success or attempt == 5

		if success then
			print("Data saved for", Player.Name)
		else
			warn("Unable to save for", Player.Name)
		end
	end
end

Players.PlayerAdded:Connect(PlayerAdded)



Players.PlayerRemoving:Connect(PlayerRemoving)


function ServerShutdown()
	print("Server shutting down.")
	for i, player in ipairs(Players:GetPlayers()) do
		task.spawn(function()
			PlayerRemoving(Player)
		end)
	end
end

game:BindToClose(ServerShutdown)


Make that the loop is entered and that the conditions inside the repeat loop are being fulfilled. It may not be reached or an error may be thrown beforehand if SetAsync is not reporting success.

Make that you are not going over any DataStore limits (such as write limits, which are 1 write per player every 6 seconds) and that your game is authorized to access the DataStore.

While it is appropriate to utilize the pcall function for error handling, it may halt execution if an error is thrown and not caught. Ensure that every potential error is dealt with and recorded.

Make that the PlayerRemoving and PlayerAdded functions are linked to the corresponding PlayerAdded and PlayerRemoving events. The related code will not execute if they are not firing.

To follow the course of execution, insert print statements into your script at different intervals. This can assist you in figuring out whether a loop is iterating as intended or whether a function is not being called.

When “Assigning default data” is printed, the first if statement inside PlayerAdded ends up in the wrong location. The block that you are setting playerData in should be enclosed by it.

A typo exists in the ServerShutdown method; in the PlayerRemoving call, it should say player rather than player.

To make sure they are operating as intended, independently test the SetAsync and GetAsync calls in a safe setting.

You may be hitting rate constraints if the script is being executed repeatedly quickly apart (for example, if players join and exit the game quickly). Make sure the reasoning takes this into consideration.

Look through the Roblox Developer Hub documentation for details on how the DataStore works, and browse the forums for discussions of related problems that other developers have encountered and fixed.

Note that I am not an expert coder, therefore there’s a chance I made some mistakes. Even though I tried my hardest to assist you, please make sure you obtain expert assistance.

1 Like

What semi said and from my understanding, datastore saving works a bit better in live-game

1 Like

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