I am trying to achieve, where a player leaves the game. The script, for the auto save, saves their progress so when they rejoin they return to the location they left the game at. However, the script I made is not working. Please help.
The script:
Summary
local ds = game:GetService (“DataStoreService”)
local ds1 = ds:GetDataStore (“PositionSavingSystem”)
game.Players.PlayerAdded:connect(function(player)
local val = Instance.new(“Vector3Value”, player)
val.Name = “Position”
local userdata
pccall(funtion()
userdata = ds1:GetAsync (player.UserId)
end)
if userdata then
game.Workspace:WaitForChild(player.Name):WaitForChild("HumanoidRootPart").CFrame.new(userdata.X, userdata.y, userdataz)
else
ds1:SetAsync(player.UserId, {
x = 0,
y = 3,
z = 0
})
end
end)
game.Players.PlayerRemoving:connect(function(player)
ds1:SetAsync(player.UserId, {
x = player.Position.Value.X,
y = player.Position.Value.Y,
z = player.Position.Value.Z
})
end)
First of all, the code to be reposition your character isn’t going to work, and also you can just get the player using player.Character, but what you did isn’t advised if you’re making clones of yourself in game.
local char = player.Character
local hrp = char:WaitForChild("HumanoidRootPart")
hrp.CFrame = CFrame.new(userdata.x, userdata.x, userdata.z)
Also, it is advised to use :BindToClose to save everyone’s data before the server shuts down.
game:BindToClose(function()
for _,v in pairs(game.Players:GetPlayers()) do
ds1:SetAsync(v.UserId, {x = v.Position.Value.X, y = v.Position.Value.Y, z= v.Position.Value.Z})
end
end)
Finally, you should create an auto-save feature that saves everyone’s data at intervals
I tried your script,
It did not work. Pretty confused right now, as I don’t script much.
edit:
I’m not sure, if I placed the script wrong. The script you made I made a script and put it into, ServerScriptServices. It may needed to have gone in StarterGui. I am not sure, thanks though.
Storing data in a table in the format {x = 1, y = 1, z = 1} will just return nil if you try to print the contents of each position in the table. You can try just storing the raw values such as {1, 1, 1} and rely on x being table[1], y being table[2] and so on, or utilize a dictionary.
There may be an error at saving, the player object may already been destroyed from the game, you can try to save Player.Position value in ServerStorage or ReplicatedStorage to see if that fixes the problem.