Data Migration Not Working (Need Help)

EDIT:

I have fixed the data saving by disabling data migration and the World Leaderstats script, so now the player spawns where they last left off, but now I need the migration and the World Leaderstats to work without breaking the saving system, so if anyone has a solution please let me know!!!

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 don’t fit well together. Either “Levels” won’t save, and “World” won’t be displayed, or “Levels” won’t be displayed, and only “World”.

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!

2 Likes

Why isn’t your world leaderstats script combined with your data saving one?

I’ve tried that, but when adding it right below the level leaderstats, it would either display both world and level but wouldn’t count the levels when touching the level part, or it would display the level count in the worlds column and wouldn’t display the level column.

1 Like