How to use DataStore2 to save Player.Teams

hello, I am currently developing a game with checkpoints that uses the teams service to change checkpoint.

I have a regular DataStore script that works ‘most’ of the time, its the times that it doesn’t work that
are really frustrating.

I would like to make the change to DataStore2 because of the improved reliability.

I would appreciate some help to implement this.

Here’s the regular datastore script as a starting point (this is not my own work) its the best
I have found for the job, when it loads it sometimes reports (with the prints) at a stage and then spawns you back at the start.

local DataStore = DataStoreService:GetDataStore("GameSave1")


game.Players.PlayerAdded:Connect(function(player)
	local data 
	local success, errorMessage = pcall(function()
		data = DataStore:GetAsync(player.UserId.."-stage")
		print(data)
		print(player.Team)
		print(game.Teams[data])
	end)
	if success then 
		if data then	
			player.Team
			= game.Teams[data]
		else
			player.Team
			= game.Teams["Start"]
			print("Loading Failed")
		end
	else
		player.Team
		= game.Teams.Start
	end
	player:LoadCharacter()
end)
print("Got your data!")


game:BindToClose(function()	
	game.Players.PlayerRemoving:Connect(function(player)
		local teamName = player.Team.Name
		print("You left the game")
		local success, errorMessage = pcall(function()
			DataStore:SetAsync(player.UserId.."-stage",teamName)
		end)
		print("All went well... maybe")
		if success then 
			print("All went well officially")
		else
			print(errorMessage)
		end
	end)
	wait(3)
end)

any help or advice would be appreciated
thanks

2 Likes

Hello, Just to update the post I forgot to say what I have tried.

I have an example of script of DataStore2 that makes a leaderstat ‘Cash’ and increments it
and saves on the update increment. This works well and saves reliably.

The problem is that I need to get the Player.Teams data into this script.

local DataStore2 = require(ReplicatedStorage:WaitForChild("MainModule"))

local cashDefault = 50
	
game.Players.PlayerAdded:Connect(function(player)
	local cashDataStore = DataStore2("cash", player)
	
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = 'leaderstats'
	leaderstats.Parent = player
	
	local cash = Instance.new("IntValue")
	cash.Name = 'Cash'
	cash.Parent = leaderstats
	
	local function cashUpdate (newValue)
		cash.Value = cashDataStore:Get(newValue)
	end
	
	
	cashUpdate(cashDefault)
	
	cashDataStore:OnUpdate(cashUpdate)
	
	while true do
		wait(2.5)
		cashDataStore:Increment(25, cashDefault)
	end
	
end)

Thanks

Should I concentrate on improving the reliability of the datastore 1.

If this is unachievable with DataStore2

I think this should work.

-- if a player joins for the first time, they will be teamed to 'defaultTeam'
-- set it to the name of the team not the team object itself
local defaultTeam = game.Teams.Start.Name

game.Players.PlayerAdded:Connect(function(player)
    local stageDataStore = DataStore2("stage", player)
    local data = stageDataStore:Get(defaultTeam)
    player.Team = game.Teams:FindFirstChild(data)
    player:LoadCharacter()
end

game.Players.PlayerRemoving:Connect(function(player)
    local stageDataStore = DataStore2("stage", player)
    local newTeam = player.Team.Name
    stageDataStore:Set(newTeam)
end
1 Like

Thank you so much, I have tested and it works. I am now using the latest DataStore2 and its working well.

1 Like

Sometimes it dose misfire and doesn’t get the save data. Then it spawns you back at the start. Could any checks be added?