Datastore returns Nil?

Really confused as to why it does not work. If anyone could help me it is greatly appreciated!

local DataStoreService = game:GetService("DataStoreService") 
local DataStore = DataStoreService:GetDataStore("BanSystem158192575215") 

game.Players.PlayerAdded:Connect(function(plr)
	local Data	
	local Player_ID = "Player_" .. plr.UserId

	local Success, err = pcall(function()
		Data = DataStore:GetAsync(Player_ID)
	end)

	if not Data then
		Data = {
			BanWave = false,
			Banned = false,
			Unbanned = false,
			LastLogged = os.time()
		}
	end

	print(Data) -- Prints Data
end)

game:BindToClose(function()
	for i, plr in pairs(game.Players:GetPlayers()) do
		local Data	
		local Player_ID = "Player_" .. plr.UserId

		local Success, err = pcall(function()
			Data = DataStore:GetAsync(Player_ID)
		end)
		
		Data.LastLogged = os.time()

		local success,err = pcall(DataStore.UpdateAsync, DataStore, "Player_"..plr.UserId, function()
			return Data
		end)            
		
		print(Data) -- Prints Nil

		if not success then error(err) end
	end
end)

game.Players.PlayerRemoving:Connect(function(plr)
	local Data	
	local Player_ID = "Player_" .. plr.UserId

	local Success, err = pcall(function()
		Data = DataStore:GetAsync(Player_ID)
	end)
	
	Data.LastLogged = os.time()

	local success,err = pcall(DataStore.UpdateAsync, DataStore, "Player_"..plr.UserId, function()
		return Data
	end)            
	
	print(Data) -- Prints Nil

	if not success then error(err) end
end)

Output:

Screen Shot 2022-10-20 at 7.58.24 PM

You set the 'data' variable with the data template but you never save it! So after you set the new data template to the 'data' variable (in your code the green box I pictured below) you would need to save it right after.
image

Can’t believe I missed something so simple! Thank you :slight_smile: