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

Get player position when player leaves?

Asked by
RjsMc 48
5 years ago

Now I have tried doing this and it is part of a data storage. However, I have no idea how to get player position when they leave. Here is what I tried:

game.Players.PlayerRemoving:Connect(function(player)

game.Players:FindFirstChild(player.Name).Location.X.Value = game.Workspace:FindFirstChild(player.Name).HumanoidRootPart.Position.X

game.Players:FindFirstChild(player.Name).Location.Y.Value = game.Workspace:FindFirstChild(player.Name).HumanoidRootPart.Position.Y

game.Players:FindFirstChild(player.Name).Location.Z.Value = game.Workspace:FindFirstChild(player.Name).HumanoidRootPart.Position.Z

end)

These are coordinates for the player when they leave which I hoped would get their location when they leave. Obviously, it does not work. How would I do this?

2 answers

Log in to vote
0
Answered by 5 years ago
local Players = game:GetService("Players")

local function onCharacterAdded(character)

end

local function onCharacterRemoving(character)
    print(character.HumanoidRootPart.Position.X)
    print(character.HumanoidRootPart.Position.Y)
    print(character.HumanoidRootPart.Position.Z)
end

local function onPlayerAdded(player)
    player.CharacterAdded:Connect(onCharacterAdded)
    player.CharacterRemoving:Connect(onCharacterRemoving)
end

Players.PlayerAdded:Connect(onPlayerAdded)

This script appears to function perfectly. Try using this instead of directly doing game.Players.PlayerRemoving?

0
I dont think you can't use a characterremoving function for PlayerAdded RjsMc 48 — 5y
1
@RjsMc You can actually; it fires right when the player leaves, however this code should check to differentiate a player respawn from a player removal, as CharcterRemoving will fire if the player resets their character as well. DirectorDexter 69 — 5y
Ad
Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

what i'd use is datastores to save and load positions. Try this it might work, I am very lazy to test it out but it just might work / give you a rough idea on how to save the players position when they leave.

local data = game:GetService('DataStoreService'):GetDataStore('data')
game.Players.PlayerAdded:Connect(function(p)
    local fetchedD = data:GetAsync(p.UserId) -- loading.
    if fetchedD ~= nil then
        for _, v in pairs(fetchedD) do
            if v then
                p.Character.HumanoidRootPart.Position = Vector3.new(fetchedD[1],fetchedD[2],fetchedD[3])
            else
                print('no data')
            end
        end
    end
end)
game.Players.PlayerRemoving:Connect(function(p)
    function save()
        local table = {} -- saving all the data.
        local pos = p.Character.HumanoidRootPart.Position
        table.Insert(table,pos.X)
        table.Insert(table,pos.Y)
        table.Insert(table,pos.Z)
    end
    save()
end)
game:BindToClose(function()
    save()
end)

So this is my rough assumption, there might be an error where there is BindToClose but other than that, this script should work.

Remember if this helped at all, click the accept button, thanks, bye C:

0
ServerScriptService.DataSave.JoinedandLocation:4: bad argument #1 to 'pairs' (table expected, got nil) RjsMc 48 — 5y
0
try now greatneil80 2647 — 5y

Answer this question