Hi, I'm making an obby and I'm trying to make a script that saves the position when a player leaves and puts them in the same position when they rejoin. It just keeps on bringing me back to the original SpawnLocation. Please help!
Code:
DataStoreService = game:GetService("DataStoreService")
positionStore = DataStoreService:GetDataStore("positionStore")
game.Players.ChildAdded:Connect(function(plr)
local success, position = pcall(function()
return positionStore:GetAsync(plr)
end)
if not position then
local success2, err2 = pcall(function()
positionStore:SetAsync(plr, tostring("-2, 1.5, 54"))
end)
if success2 then
print("Successfully saved position for " .. plr.Name .. "!")
elseif err2 then
error("Error saving position for " .. plr.Name .. ". Error: " .. err2)
end
else
local character = plr.CharacterAdded:Wait()
character:MoveTo(Vector3.new(tonumber(position)))
end
end)
game.Players.PlayerRemoving:Connect(function(plr)
local success3, err3 = pcall(function()
positionStore:SetAsync(plr, tostring(plr.Character.Position))
end)
if success3 then
print("Successfully saved data for " .. plr.Name .. "!")
elseif err3 then
error("Error saving data for " .. plr.Name .. ". Error: " .. err3)
end
end)

1 comment
Im not sure if this would help your case, but if you could substitiute the SpawnLocation with a normal Part and just use the PlayerAdded function, and when the player touches the part (checkpoint), then you would save the players' position. Hope this helps!