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!