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?
