How fix problem in obby

So that the player appears at the last stage where he reached

Please write to someone alone, I can’t answer everyone at once

Try running the oa function as the player’s character has been added.

Maybe:

game.Players.PlayerAdded:Connect(function(player)
  player.CharacterAdded:Connect(function()
	print("connected")
	local playerFolder = Instance.new("Folder")
		playerFolder.Name = "leaderstats"
		playerFolder.Parent = player

	local ack2 = Instance.new("IntValue")
		ack2.Name = "Stage"
		ack2.Value = 1
		ack2.Parent = playerFolder
--loaddata	
	local savestuff = ds:GetAsync(player.UserId)	
	--print("data takes up ".. game:GetService("HttpService"):JSONEncode(savestuff):len().. "/260000")
local StatsFile = player.leaderstats
	if savestuff then
		for i,v in pairs(savestuff) do
			if i % 2 == 0 then
				StatsFile:FindFirstChild(savestuff[i - 1]).Value = v
				
			end
		end
		
	end
       oa()
   end)
end)

Make sure to define the function oa before this part of the script.

I tried three different datastores and they all have the same problem

It’s not a datastore problem, the problem is that the oa function which moves a player to the last stage they were at is not called when they join the game.

  1. Instead of ChildAdded, just put CharacterAdded, and put it inside the PlayerAdded function
  2. Players:playerFromCharacter() is deprecated, replace it with Players:GetPlayerFromCharacter()

Since you said it works until you reset, if you get the CharacterAdded event before the player respawns, it should fire.

What needs to be do so that the player appears at the stage where he reach

I already told you what you should try.

Your final script should look like this then:

-- :3
--DataStore--
local ds = game:GetService("DataStoreService"):GetDataStore("StatsStore4")

function oa(object)
	local player = game.Players:GetPlayerFromCharacter(object)
	if player ~= nil then
		local ls = player.leaderstats
		local sl = game.Workspace.Checkpoints:FindFirstChild(ls.Stage.Value)
		print("gah")
		wait(2)
		object.Torso.CFrame = object.Torso.CFrame + Vector3.new(0,3,0)
		object.Torso.CFrame = sl.CFrame + Vector3.new(0,3,0)
	end 
end

game.Players.PlayerAdded:Connect(function(player)
  player.CharacterAdded:Connect(function()
	print("connected")
	local playerFolder = Instance.new("Folder")
		playerFolder.Name = "leaderstats"
		playerFolder.Parent = player

	local ack2 = Instance.new("IntValue")
		ack2.Name = "Stage"
		ack2.Value = 1
		ack2.Parent = playerFolder
--loaddata	
	local savestuff = ds:GetAsync(player.UserId)	
	--print("data takes up ".. game:GetService("HttpService"):JSONEncode(savestuff):len().. "/260000")
local StatsFile = player.leaderstats
	if savestuff then
		for i,v in pairs(savestuff) do
			if i % 2 == 0 then
				StatsFile:FindFirstChild(savestuff[i - 1]).Value = v
				
			end
		end
		
	end
       oa()
   end)
end)

local function saveValues(player)
	local saveData = {}
	local StatsFile = player.leaderstats

	for i,v in pairs(StatsFile:GetChildren()) do
			saveData[#saveData + 1] = v.Name
			saveData[#saveData + 1] = v.Value
	end
	ds:SetAsync(player.UserId, saveData)
end

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

game.Players.PlayerRemoving:Connect(function(player)
	saveValues(player)
end)

game.Workspace.ChildAdded:connect(oa)
1 Like

I did as you said but the problem is the same

Oh now it works, I forgot some lines and now everything works