Save Location Issue

My destination is creating a Script that saves and Loads the player’s position when they join the game. However, the script doesn’t seem to save anything, but the value updates. I am currently making my first try at solving this issue and it’s to get help from the Devforum. Is anyone offering to help? Any help would be appreciated.

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

local ds = game:GetService("DataStoreService")
local ds1 = ds:GetDataStore("PositionSavingSystem")

game.Players.PlayerAdded:Connect(function(player)
	local pos = player:FindFirstChild("Position")
	if not pos then
		pos = Instance.new("Vector3Value", player)
		pos.Name = "Position"
	end

	local userData
	local success, error = pcall(function()
		userData = ds1:GetAsync(player.UserId)
	end)

	if success and userData then
		local humanoidRootPart = player.Character and player.Character:FindFirstChild("HumanoidRootPart")
		if humanoidRootPart then
			humanoidRootPart.CFrame = CFrame.new(userData.x, userData.y, userData.z)
		end
	else
		ds1:SetAsync(player.UserId, {
			x = 0,
			y = 3,
			z = 0
		})
		userData = {
			x = 0,
			y = 3,
			z = 0
		}
	end

	player.CharacterAdded:Connect(function(character)
		local humanoidRootPart = character:WaitForChild("HumanoidRootPart")
		while true do
			wait(5)
			userData = {
				x = humanoidRootPart.Position.X,
				y = humanoidRootPart.Position.Y,
				z = humanoidRootPart.Position.Z
			}
			pos.Value = humanoidRootPart.Position
		end
	end)
end)

game.Players.PlayerRemoving:Connect(function(player)
	local pos = player:FindFirstChild("Position")
	if pos then
		ds1:SetAsync(player.UserId, {
			x = pos.Value.X,
			y = pos.Value.Y,
			z = pos.Value.Z
		})
	end
end)
1 Like

You don’t need to loop every 5 seconds to save the player’s position because you already do that on the player’s removal. If you’re scared of datalose, use profileservice. In my opinion, it’s easier to use as well.

Also, add a game:BindToClose function and add task.wait(5) inside of there.

1 Like

Well, I added task.wait() and while it worked perfectly sometimes I went underground

Try that:

local ds = game:GetService("DataStoreService")
local ds1 = ds:GetDataStore("PositionSavingSystem")

game.Players.PlayerAdded:Connect(function(player)
	local pos = Instance.new("Vector3Value", player)
	pos.Parent = player.Character
	pos.Name = "Position"


	local userData
	local success, result = pcall(function()
		return ds1:GetAsync(player.UserId)
	end)

	if success and result ~= nil then
		userData = result
		local humanoidRootPart = player.Character:WaitForChild("HumanoidRootPart")
		humanoidRootPart.Vector3 = Vector3.new(userData.x, userData.y, userData.z)

	else
		local succ, err = pcall(function()
			ds1:SetAsync(player.UserId, {
				x = 0,
				y = 3,
				z = 0
			})
		end)
		
		if err or succ == false then
			warn(err)
		end
		
		userData = {
			x = 0,
			y = 3,
			z = 0
		}
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	local pos = player.Character:FindFirstChild("Position")	
	local hrt = player.Character:FindFirstChild("HumanoidRootPart")
	pos.Value.X = hrt.Position.X
	pos.Value.Y = hrt.Position.Y
	pos.Value.Z = hrt.Position.Z
	
	local succ, err = pcall(function()
		ds1:SetAsync(player.UserId, {
			x = pos.Value.X,
			y = pos.Value.Y,
			z = pos.Value.Z
		})
	end)
	
	if err or succ == false then
		warn(err)
	end
end)

Maybe it works. Just added some pcalls and deleted yoru while loop, some little things.

2 Likes

Thank you it works. I will give you the solution.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.