What would the best way to be to save a users location every time he disconnects? The Character is removed so you couldn’t use that.
Not at home to test, but see if when PlayerRemoving is called the Character still exists. If it does, just get the position of the root part.
Yeah, its not there.
You could keep a loop that stores the users location every second or something. such as
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)
You may have to ensure the player is on the map though
Write to a table every step (or whatever other interval) where the character’s position is. Don’t write if the character is nil. Have the keys be player IDs. When a player joins, see if their ID is in the table.
I was thinking of doing a loop. I’d be doing this with datastore so that example wouldn’t really work.
On the playerremoving event, just save the data at that point, as you have the location, or access it on your datastore area, just ensure you dont set the table index to nil before getting it
Ah. True, I’ll try implementing this in a few moments here and I’ll let you know the feedback.
I imagine this would equally be possible by listening for DescendantRemoving because you’ll still be able to read the relevant property out of the Instance (e.g. HumanoidRootPart).
→ Check for HumanoidRootPart, save CFrame or Position X, Y and Z values
→ Save the aforementioned values to a data store via a table
Don’t know if a combination of all this would work but hey, worth a try?
you could watch for changes in position of the humanoid root part, and save it when the player’s removed
local DataStore = game:GetService("DataStoreService"):GetDataStore("test")
wait(1)
print(DataStore:GetAsync("last"))
print(DataStore:GetAsync("time"))
local lastPos = Vector3.new(0,0,0)
game.Players.PlayerAdded:connect(function(plr)
plr.CharacterRemoving:Connect(function(char)
lastPos = char.HumanoidRootPart.Position
end)
end)
game.Players.PlayerRemoving:Connect(function(plr)
DataStore:SetAsync("last",tostring(lastPos))
DataStore:SetAsync("time",tostring(tick()))
end)
game:BindToClose(function()
wait(3)
end)
This code will save your last position (I tested multiple times and it didnt fail a single time)
(Position before i left)
-0.0158325434, 3.23737574, 6.74493551 (position printed by datastore (players position shifted a tiny bit cause of the idle animation)
You would obviously change this up to have some kind of table storing the positions for each player.
I’d recommend using Character.HumanoidRootPart
rather than UpperTorso
, as HumanoidRootPart
is compatible and present in both R6
and R15
; UpperTorso
, however, is only present in R15
.
Thanks, fixed it!