Player's Position Not Saving (DataStore2)

Hello, I’m trying to make a script that saves the player’s position when they leave. I am using Datastore2 to save the player’s data, a position is being saved, but it isn’t the player’s position?

local DataStore2 = require(1936396537)
game.Players.PlayerAdded:Connect(function(plr)
	local XDataStore = DataStore2("X",plr)
	local YDataStore = DataStore2("Y",plr)
	local ZDataStore = DataStore2("Z",plr)

	
	local char = plr.Character or plr.CharacterAdded:Wait()
	local Folder = Instance.new("Folder",plr)
	Folder.Name = "PlayerStats"
	
	local Position = Instance.new("Folder",Folder)
	Position.Name = "Position"

	local X = Instance.new("IntValue",Position)
	X.Name = "X"
	
	local Y = Instance.new("IntValue",Position)
	Y.Name = "Y"
	
	local Z = Instance.new("IntValue",Position)
	Z.Name = "Z"
	
	local function YUpdate(UpdatedValue)
		Y.Value = YDataStore:Get(UpdatedValue)
	end
	
	local function ZUpdate(UpdatedValue)
		Z.Value = ZDataStore:Get(UpdatedValue)
	end
	
	local function XUpdate(UpdatedValue)
		X.Value = XDataStore:Get(UpdatedValue)
	end
	---I set up X,Y,Z coord
	XUpdate(game.Workspace.Spawn.Position.X)
	YUpdate(game.Workspace.Spawn.Position.Y)
	ZUpdate(game.Workspace.Spawn.Position.Z)
	---when they first join they'll spawn at a certain brick
	ZDataStore:OnUpdate(ZUpdate)
	YDataStore:OnUpdate(YUpdate)
	XDataStore:OnUpdate(XUpdate)
	
	function LoadPlayer()
	for i = 1, 5 do 
		if char and char:FindFirstChild("HumanoidRootPart") then
			wait(1)
			char.HumanoidRootPart.Position = Vector3.new(X.Value,Y.Value,Z.Value)
			end
		end
	end
	--loading the player 5 times incase it takes a while to load, (i know that if the player leaves during this time it'll still save)
	spawn(LoadPlayer)
end)

game.Players.PlayerRemoving:Connect(function(plr)
	local char = plr.Character or plr.CharacterAdded:Wait()
	local XDataStore = DataStore2("X",plr)
	local YDataStore = DataStore2("Y",plr)
	local ZDataStore = DataStore2("Z",plr)
	local Position = plr:WaitForChild("PlayerStats"):WaitForChild("Position")
	if char.HumanoidRootPart then
		XDataStore:Increment(1 * 0 + plr.Character.HumanoidRootPart.Position.X,game.Workspace.Spawn.Position.X)
		YDataStore:Increment(1 * 0 + plr.Character.HumanoidRootPart.Position.Y,game.Workspace.Spawn.Position.Y)
		ZDataStore:Increment(1 * 0 + plr.Character.HumanoidRootPart.Position.Z,game.Workspace.Spawn.Position.Z)
		-- I do 1 * 0 to make all the values = 0 then i add the players coord
	end
end)

Not getting an error?

  • I don’t understand why you use the 1 * 0. You could just remove that part completely since it results in 0.
  • You should look at combining different keys under 1 key by using the :Combine() function

I don’t use increment in DataStore2 since it’s never worked for me. I just get the data, modify it and then use Set or SetTable, depending on what’s needed.

If I removed 1 * 0 it would just be adding the current position to whats already there.

Isn’t that what you want? To increment the value that’s already there with the position.

No, I want to set the values position to the characters position.

Oh, then you should not use Increment, you should use :Set() to set a value and not use the second parameter.

The code should be like this:

ZDataStore:Set(plr.Character.HumanoidRootPart.Position.Z)

When you used Increment() it would do like this: OldValue + (1 * 0 + newValue)
But with Set() it would just override the old value.

1 Like

Thanks, that fixed it! :slight_smile:

1 Like

No problem :wink: mark it as a solution so others can easily find it.

Also, how does :combine() work? (new to datastore2)

I’m not 100% sure of what it exactly does but it 's used to combine tables under 1 key. In my code I combine all my tables with 1 “master” key DATA.

A sketch would look like this:
Untitled
where the top node is like the master key that connects to all the other tables.

Practically It’s used by writing:

DataStore2.Combine("DATA", "NAME_OF_TABLE")

Before using the table NAME_OF_TABLE in this example, it could as an example be inventory, money or something else.

I believe it’s used for recovering data in case of data loss and maybe caching.

1 Like