Migrating Data Not Working

Hey everyone, I am working on a checkpoint system for my game, and I am wanting the “World” leaderstats to migrate to the lobby world (that is the parent of the world that I am trying to migrate the checkpoints from in asset manager), and my scripts aren’t working together. Either “Levels” won’t save, and “World” won’t be displayed, or “Levels” won’t be displayed, and only “World”.

I can fix the “no saving” issue by disabling the leaderstats and migrate scripts, but then of course I’m not getting what I want to work, which is all of them together.

Here are the scripts that are conflicting:

DATA SAVING SCRIPT:

local DataStoreService = game:GetService("DataStoreService")
local SaveDataStore = DataStoreService:GetDataStore("SaveData")


local function SavePlayerData(player)

	local success,errormsg = pcall(function()

		local SaveData = {}

		for i,stats in pairs(player.leaderstats:GetChildren()) do

			SaveData[stats.Name] = stats.Value
		end 
		SaveDataStore:SetAsync(player.UserId,SaveData)
	end)

	if not success then 
		return errormsg
	end   
end 


Players.PlayerAdded:Connect(function(player)

	local Stats = Instance.new("Folder")
	Stats.Name = "leaderstats"
	Stats.Parent = player

	local Level = Instance.new("StringValue")
	Level.Name = "Level"
	Level.Parent = Stats
	Level.Value = 1

	local Data = SaveDataStore:GetAsync(player.UserId)

	if Data then

		print(Data.Level)

		for i,stats in pairs(Stats:GetChildren()) do

			stats.Value = Data[stats.Name]  
		end   
	else  
		print(player.Name .. " has no data.") 
	end


	player.CharacterAdded:Connect(function(character)

		local Humanoid = character:WaitForChild("Humanoid")
		local Torso = character:WaitForChild("HumanoidRootPart")

		wait()

		if Torso and Humanoid then
			if Level.Value ~= 0 then

				local LevelPart = workspace.Levels:FindFirstChild(Level.Value)
				Torso.CFrame = LevelPart.CFrame + Vector3.new(0,1,0)     
			end 
		end 
	end)  
end)


Players.PlayerRemoving:Connect(function(player)

	local errormsg = SavePlayerData(player)

	if errormsg then 
		warn(errormsg)  
	end 
end)

game:BindToClose(function()
	for i,player in pairs(Players:GetPlayers()) do 

		local errormsg = SavePlayerData(player)
		if errormsg then
			warn(errormsg)
		end   
	end
	wait(2) 
end)

WORLD LEADERSTATS:

local Players = game:GetService("Players")
Players.PlayerAdded:Connect(function(player)
	
	local Stats = Instance.new("Folder")
	Stats.Name = "leaderstats"
	Stats.Parent = player
	
	local world = Instance.new("IntValue")
	world.Name = "World"
	world.Parent = Stats
	world.Value = 1
	
	player.CharacterAdded:connect(function(Character)
		
	end)
end)

DATA MIGRATION:

local ds = game:GetService("DataStoreService"):GetGlobalDataStore()
game.Players.PlayerAdded:Connect(function(plr)
	wait()
	local plrkey = "id_"..plr.UserId
	local save1 = plr.leaderstats.World

	local Saved = ds:GetAsync(plrkey)
	if Saved then
		save1.Value = Saved[1]
	else
		local NumberForSaving = {save1.Value}
		ds:GetAsync(plrkey, NumberForSaving)
	end
end)

game.Players.PlayerRemoving:Connect(function(plr)
	ds:SetAsync("id_"..plr.UserId, {plr.leaderstats.World.Value})
end)

LEVEL SCRIPT (within each checkpoint Part):

local Levels = workspace:WaitForChild("Levels")
for i,Level in pairs(Levels:GetChildren()) do
	Level.Touched:Connect(function(touch)
		
		local humanoid
		if touch.Parent:FindFirstChild("Humanoid") then
			humanoid = touch.Parent.Humanoid
		end
		
		if touch.Parent and touch.Parent.Parent:FindFirstChild("Humanoid") then
			humanoid = touch.Parent.Parent.Humanoid
		end
		
		if humanoid then
			local player = game.Players:GetPlayerFromCharacter(humanoid.Parent)
			
			local PlayerLevel = player.leaderstats.Level.Value
			if tonumber(Level.Name) == PlayerLevel + 1 then
				player.leaderstats.Level.Value = player.leaderstats.Level.Value + 1
			elseif tonumber(Level.Name) > PlayerLevel + 1 then
				humanoid.Health = 0
			end
		end
	end)
end

All help is appreciated!

Hi, would you mind explaining me more about the issue as I don’t really understand much… ty

1 Like

Again, sorry about the delay.

In my game, the player first spawns in at a lobby (if you’ve played Islands it’s somewhat like that in the lobby only smaller and just portals). The player can then walk through the first world if it’s their first time playing, which then teleports them to a world that is connected through Asset Manager.

The player is then navigating through mazes, and there’s a basic checkpoint system that saves when the player touches a checkpoint block. The leaderstats for the game are within that save script as you may have noticed, and that displays what level block the player has touched, displaying, for example “Level 6”. What I’m wanting to do is also have what WORLD you’re in (such as World 1) be displayed on the leaderboard, and then migrate that data to that portal lobby world so that way players can see what world you’re on, and also be able to unlock other world portals.

The issue I’m running into is that I can’t get both Levels and World to be displayed at the same time with the levels saving. Like I mentioned in the post, the World count will be displayed, but using the amount of levels you’ve gained, or levels will be displayed but won’t save, etc. I’ve tried adding the Worlds leaderstats script into the Save script where the Levels leaderstats are, but that doesn’t work.

Sorry for the length of this :sweat_smile: If you need some more clarification on any other aspect of this, then please let me know.