Save Location Problem

Hello, I have this script that saves the last players location. When I leave from the game and I rejoin ti works good. But when I publish the game from studio and i join from roblox I respawn. Can someone help me, so when I publish the game the savelocation will still work without any problem?

local store = DataStore:GetDataStore("PlayerCharacterInfo") -- don't change the name of the DataStore or all data on it will be reset
local joined = false
game.Players.PlayerAdded:Connect(function(plr) --runs when the player joins
	plr.CharacterAdded:Connect(function() --waits for the player character to be added to the game
		if joined == false  then
			joined = true
			--store:RemoveAsync(plr.UserId) --This code will Delete the last location just in case you want to reset a player's data for testing
			local playerdata = store:GetAsync(plr.UserId)
			if playerdata  then --tests if the player joined the game before
				plr.Character.HumanoidRootPart.CFrame = CFrame.new(playerdata[1], playerdata[2], playerdata[3]) --updats the player's Posistion if they joined before
			end
			wait(4)
			while wait() and plr do --updates the play's Position every few seconds (5-15 seconds)
				if plr then
				local save = {}
				table.insert(save,plr.Character.HumanoidRootPart.CFrame.Position.X)
				table.insert(save,plr.Character.HumanoidRootPart.CFrame.Position.Y)
				table.insert(save,plr.Character.HumanoidRootPart.CFrame.Position.Z)
				store:SetAsync(plr.UserId,save)
					end --if character loaded
			end --while loop
			
			
			
			
		end -- if they just joined
	end)
end)
game.Players.PlayerRemoving:Connect(function(plr)
	
end)

I have encountered a similar issue before when it wouldn’t fire CharacterAdded() when the player first joins so I do something similar to this inside of the player added function:

for i=1, 100 do
if plr.Character then
break
else
wait(0.5)
end

You would then need to add the code for when the character loads in

The for loop delays anything from occurring before the character has loaded it prevents the script from not detecting the character loading.

You would also need to keep the original code for when the player respawns.

You could try to use game:BindToClose() to add a wait function so that data will finish saving before the server closes as this can cause data to not finish saving.

Here is an example:

game:BindToClose(function()
wait(5)
end)

Also, you could try adding a wait() function in the while loop to provent any errors from too many SetAsync() requests.

You may also need to enable Enable Studio Access To API Services in the game settings in studio as this won’t save any data to roblox when testing in studio.

Final note: the joined variable should go into the PlayerAdded event otherwise it will not load the location data for other players who join the game as it will read the joined variable as true.

Sorry for the indentation of the code being weird.

Hope this helps :slight_smile:

I will check it out tomorrow. To see if it works.