local DataStoreService = game:GetService("DataStoreService")
local locationStore = DataStoreService:GetDataStore("LocationStore")
game.Players.PlayerAdded:Connect(function(player)
local character = game.Workspace:WaitForChild(player.Name)
local data
local success, err = pcall(function()
data = locationStore:GetAsync(player.UserId.."-position")
end)
if success then
if data and data ~= {x = 0, y = 3, z = 0} then
character.HumanoidRootPart.Position = Vector3.new(data.x, data.y, data.z)
else
print("No data found!")
locationStore:SetAsync(player.UserId.."-position", {
x = 0,
y = 3,
z = 0
})
end
else
warn("There was an error while getting data!")
warn("Error code:", err)
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local character = game.Workspace:WaitForChild(player.Name)
local success, err = pcall(function()
locationStore:SetAsync(player.UserId.."-position", {
x = character.HumanoidRootPart.Position.X,
y = character.HumanoidRootPart.Position.Y,
z = character.HumanoidRootPart.Position.Z
})
end)
if success then
print("Data successfully saved!")
else
warn("There was an error while saving data!")
warn(err)
end
end)```
Any error? If so, please post it.
EDIT: I’ve tested it out and the issue is the character is removed before you go to save the data on player removing. To fix this, have a IntValue object for X,Y, and Z located within the player and update the values to keep them up to date. On removing, save those coordinates.
If you could show what the output looks like it’d be much easier to find the issue!