Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How would i make a script that saves the players position when they disconnect?

Ok so, i want to make an script that saves a players position when they disconnect from the game and when they join back, they are in the same position as they were left. Please help!

local locations = {}

while wait(1) do
  for _,player in pairs(game.Players:GetChildren()) do 
    if player and player.Character and player.Character.Parent ~= nil then
      locations[player] = player.Character.Torso.Position
    end
  end
end

game.Players.PlayerRemoving:connect(function(player)
  local lastlocation = locations[player]
  locations[player] = nil -- garbage collection
end)

I tried this but, it obviously wouldn't work. :(

1 comment

0

hmmm

1 answer

local LastPositionDataStore = game:GetService("DataStoreService"):GetDataStore("LastPos")

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(char)
		local HRP = char:WaitForChild("HumanoidRootPart")
		HRP.Position = 0,3,0 or LastPositionDataStore:GetAsync(player.UserId) --Gets the last position of the player's HumanoidRootPart position
	end)
end)

game.Players.PlayerRemoving:Connect(function(player)
	local char = player.Character
	local HRP = char:WaitForChild("HumanoidRootPart")
	LastPositionDataStore:SetAsync(player.UserId,HRP.Position) --Saves the last position of the player's HumanoidRootPart position
end)

4 comments

0

Hmm. I don't know why but it's not working. Do i have to test it through roblox player? (Not roblox studio?)

0

Yea you need to test it from roblox player

0

I don't think you can get the position of a part of the player's character when they leave

0

is the script able to get the HRP before the character gets destroyed?

This discussion is preserved as it was. New answers, comments, and votes are closed.