Hello! I'm making an obby and currently I have two scripts that saves the players stage. Currently everything works, the only problem I have is that when the player leaves and rejoins they don't spawn at their last checkpoint, they spawn at the neutral spawnpoint. How can I fix this?

Stage Script:

local Stages = workspace:WaitForChild("Stages")

for i, Stage in pairs(Stages:GetChildren()) do

    Stage.Touched:Connect(function(touch)

        local humanoid

        local player = game.Players:GetPlayerFromCharacter(touch.Parent)

        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 and player then
            local PlayerStage = player.leaderstats.Stage.Value

            if tonumber(Stage.Name) == PlayerStage + 1 then

                player.leaderstats.Stage.Value = player.leaderstats.Stage.Value + 1
				player.leaderstats.Total.Value = player.leaderstats.Total.Value + 1
			end
        end
    end)
end

Here's the autosaving script which might help.

Saving Script:

local Players = game:GetService("Players")
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 Stage = Instance.new("IntValue")
	Stage.Name = "Stage"
	Stage.Parent = Stats
	Stage.Value = 0
	
	local Total = Instance.new("IntValue")
	Total.Name = "Total"
	Total.Parent = Stats
	Total.Value = 0
	
	local Prestige = Instance.new("IntValue")
	Prestige.Name = "Prestige"
	Prestige.Parent = Stats
	Prestige.Value = 0
	
	local Data = SaveDataStore:GetAsync(player.UserId)
	
	if Data then
			
		print(Data.Stage)
		
		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 Stage.Value ~= 0 then
				
				local StagePart = workspace.Stages:FindFirstChild(Stage.Value)
				Torso.CFrame = StagePart.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)

2 comments

0

Do you get any errors?

0

No.