i made this script that i supposed to save your team when you leave the game. for some reason the script does not work.

script:

local DataStoreService = game.GetService("DataStoreService")

local DataStore = DataStoreService:GetDataStore("ObbyDataStore")

game.Players.PlayerAdded:Connect(function(player)

local data

local success, errorMessage = pcall(function()
	data = DataStore:GetAsync(player.UserId.."-stage")
end)

if success then
	print("Success")
	if data then
		print("Test1") 
		player.Team = game.Teams[data]
	else
		print("Test2")
		player.Team = game.Teams.Lobby
	end
else
	print(errorMessage)
	player.Team = game.Teams.Lobby
end

player:LoadCharacter()

end)

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

local teamName = player.Team.Name

local success, errorMessage = pcall(function()
	DataStore:SetAsync(player.UserId.."-stage",teamName)
end)

if success then
	print("All went well")
else
	print(errorMessage)
end

end)