Auto Save teleport not working.. | Please help!

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)

Thanks for any help!

3 Likes

There are some misstypes in your script I’m not sure if that is the problem or that’s just the post
like pccall

pccall, is in the script. At least, I think it should be… I got a little bit of the script from a YouTube tutorial.

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

1 Like

@raretendoblox

I tried your script,
It did not work.
:confused: 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.

You can’t store userdata in DataStoreService. Try to save the XYZ of the position.

Okay, thanks… @smallpenguin666
(30characters)

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.

Therefore:

ds1:SetAsync(player.UserId, {["x"] = 1, ["y"] = 5, ["z"] = 3})

And you would set the player’s position when they join the game using this where your current if statement for userdata is:

if userdata then
   player.Character:MoveTo(Vector3.new(userdata["x"], userdata["y"], userdata["z"]))
else

Read more on how to use tables and dictionaries properly here.

2 Likes

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.