Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by
Zripple 18
6 years ago

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. :(

0
hmmm thedogegamer22 -44 — 6y

1 answer

Log in to vote
0
Answered by
HaveASip 494 Moderation Voter
6 years ago
Edited 6 years ago
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)
0
Hmm. I don't know why but it's not working. Do i have to test it through roblox player? (Not roblox studio?) Zripple 18 — 6y
0
Yea you need to test it from roblox player HaveASip 494 — 6y
0
I don't think you can get the position of a part of the player's character when they leave theking48989987 2147 — 6y
0
is the script able to get the HRP before the character gets destroyed? hellmatic 1523 — 6y
Ad

Answer this question