Data not working

I’m trying to make a simulator game, but my Data is not setting properly. It’s not making it anywhere in the pcall, and both print statements don’t print. (it doesnt print if u have data, or if you don’t)

Data.Start = function(player)
	if player then
		local function setup()
			local success, err = pcall(function()
				if not GameData:GetAsync(player.UserId .. '-GameData') then
					print('indexing game data')
					Data.SessionData[player] = {
						Data = {
							Inventory = {},
							Histories = {
								Trade = {};
							};
							Currencies = {
								Clicks = 0,
								Rebirths = 0,
								SuperRebirths = 0,
								Tokens = 0, 
								Electrons = 0,
								Upgrades = {

								}
							}
						},
						LastestLogin = os.time();
						Rewards = {
							Daily = {lastClaim = false, didClaim = false},
							Chest1 = {lastClaim = false, didClaim = false},
							Chest2 = {lastClaim = false, didClaim = false};
						}
					}
				else
					print('indexing exist data')
					Data.SessionData[player] = GameData:GetAsync(player.UserId..'-GameData')
				end
			end)
			if err then
				setup()
				warn(err)
			end
		end
		setup()
	end
end

where is this called seems some of the script is missing or you are trying to call setup inside of the setup function which will never happen??

Data.Start = function(player)

Also you should try to not use your GetAsync in an if statment then call again below if else this will be multi calls that are not require set it as a variable first then check in if statment

I call it with playerAdded, passing the player:

game.Players.PlayerAdded:Connect(function(player)
	MainHandlers[player] = true
	--// DATA STARTING \\
	local PlayerData = nil
		while wait() do
			Data.Start(player)
			PlayerData = Data.Collect(player)
			if PlayerData then
				print('Data success!...')
				break
			end
			if PlayerData == nil then
				print('Data failed! Repeating...')
			end
		end
end)

with it in this while loop this may timeout your datastores if it keeps firing

Also do you get any errors?

No errors. It just prints that Data failed! Repeating… indicating its still nil

this needs to return something where is it at?

Also you may want to try this reworked script from above you posted this will work better on loading the data I included a return which will return the data when the Data.Start is called you may need to do this on the Data.Collect since its not returning

	local success, err = pcall(function()
		Data.SessionData[player] = GameData:GetAsync(player.UserId .. '-GameData')
	end)
	if success then
		if not Data.SessionData[player] then  -- no data so setup default data for this player
			print('indexing game data')
			Data.SessionData[player] = {
				Data = {
					Inventory = {},
					Histories = {
						Trade = {};
					};
					Currencies = {
						Clicks = 0,
						Rebirths = 0,
						SuperRebirths = 0,
						Tokens = 0, 
						Electrons = 0,
						Upgrades = {

						}
					}
				},
				LastestLogin = os.time();
				Rewards = {
					Daily = {lastClaim = false, didClaim = false},
					Chest1 = {lastClaim = false, didClaim = false},
					Chest2 = {lastClaim = false, didClaim = false};
				}
			}
		else
			Data.SessionData[player] = Data  -- there is data just set it to main table
		end
	else
		warn('Data not able to load for player:  ' .. player.Name)
	end
	return Data.SessionData[player]   -- if there is data it will return if not returns nil
end

Also prints are you friend in debugging to test your code above you could print out the PlayerData and see

Also getting data in a while loop can cause timeout issues since it never ends you should use repeat with counter or while with counter once hit say 10 then break out loop or return

Data.Collect is:


Data.Collect = function(player)
	return Data.SessionData[player]
end

ok with return on the start you don’t need that

Well I use that to get data in the future, so I dont have to run .start everytime.

a way to get around needing to get data in future is by setting your data as values on your player or as attributes then all clients and server can access your players data

if a server script changes the data it can instantly be displayed or read on all clients and server also your save script can catch the change update your Data.SessionData table for that players data then on your next save loop save that player data or on leave if it hasn’t been saved since last change

– hold a changed table for each player like Changed = {}
Changed[player] = true – then check like

if Changed[player]  then
-- run save here 
Changed[player] = nil  -- do this on leave also after saving
end