Hello,
I’m trying to save the players’ position with ProfileService when they leave, and I’m using a player removing event to save it after they leave, but the problem is the character disappears too fast so it’s nil before it can save the cframe
I’ve tried all that I can think of but none of it worked, any help would be appreciated tremendously
local Players = game.Players
local ServerScriptService = game.ServerScriptService
local ProfileService = require(ServerScriptService.ProfileService)
local Manager = require(ServerScriptService.ProfileService.Manager)
local Template = require(ServerScriptService.ProfileService.Template)
local ProfileStore = ProfileService.GetProfileStore('MainData', Template)
local function playerAdded(plr:Player)
local Profile = ProfileStore:LoadProfileAsync('Player_'..plr.UserId)
if Profile == nil then
plr:Kick('Unable to find data? Please rejoin.')
return
end
Profile:AddUserId(plr.UserId)
Profile:Reconcile()
Profile:ListenToRelease(function()
Manager.Profiles[plr] = nil
plr:Kick('Unable to find data? Please rejoin.')
end)
if plr:IsDescendantOf(Players) == true then
Manager.Profiles[plr] = Profile
else
Profile:Release()
end
end
for _, player in Players:GetPlayers() do
task.spawn(playerAdded,player)
local profile = Manager.Profiles[player]
print(profile.Data.Position)
end
game.Players.PlayerAdded:Connect(playerAdded)
game.Players.PlayerRemoving:Connect(function(player:Player)
local Profile = Manager.Profiles[player]
local PlayerPosition = { player.Character.PrimaryPart.CFrame:GetComponents() } -- where it returns nil
Profile.Data.Position = PlayerPosition
if not Profile then return end
Profile:Release()
end)
local Players = game.Players
local ServerScriptService = game.ServerScriptService
local ProfileService = require(ServerScriptService.ProfileService)
local Manager = require(ServerScriptService.ProfileService.Manager)
local Template = require(ServerScriptService.ProfileService.Template)
local ProfileStore = ProfileService.GetProfileStore('MainData', Template)
local function playerAdded(plr:Player)
local Profile = ProfileStore:LoadProfileAsync('Player_'..plr.UserId)
if Profile == nil then
plr:Kick('Unable to find data? Please rejoin.')
return
end
Profile:AddUserId(plr.UserId)
Profile:Reconcile()
Profile:ListenToRelease(function()
Manager.Profiles[plr] = nil
plr:Kick('Unable to find data? Please rejoin.')
end)
if plr:IsDescendantOf(Players) == true then
Manager.Profiles[plr] = Profile
else
Profile:Release()
end
end
for _, player in Players:GetPlayers() do
task.spawn(playerAdded,player)
local profile = Manager.Profiles[player]
print(profile.Data.Position)
end
game.Players.PlayerAdded:Connect(playerAdded)
game.Players.PlayerRemoving:Connect(function(player:Player)
local Profile = Manager.Profiles[player]
-- Change below
repeat task.wait() until { player.Character.PrimaryPart.CFrame:GetComponents() } ~= nil task.wait()
local PlayerPosition = { player.Character.PrimaryPart.CFrame:GetComponents() }
Profile.Data.Position = PlayerPosition
if not Profile then return end
Profile:Release()
end)
local Players = game.Players
local ServerScriptService = game.ServerScriptService
local ProfileService = require(ServerScriptService.ProfileService)
local Manager = require(ServerScriptService.ProfileService.Manager)
local Template = require(ServerScriptService.ProfileService.Template)
local ProfileStore = ProfileService.GetProfileStore('MainData', Template)
local function playerAdded(plr:Player)
local Profile = ProfileStore:LoadProfileAsync('Player_'..plr.UserId)
if Profile == nil then
plr:Kick('Unable to find data? Please rejoin.')
return
end
Profile:AddUserId(plr.UserId)
Profile:Reconcile()
Profile:ListenToRelease(function()
Manager.Profiles[plr] = nil
plr:Kick('Unable to find data? Please rejoin.')
end)
if plr:IsDescendantOf(Players) == true then
Manager.Profiles[plr] = Profile
else
Profile:Release()
end
end
for _, player in Players:GetPlayers() do
task.spawn(playerAdded,player)
local profile = Manager.Profiles[player]
print(profile.Data.Position)
end
game.Players.PlayerAdded:Connect(playerAdded)
game.Players.PlayerRemoving:Connect(function(player:Player)
local Profile = Manager.Profiles[player]
local PlayerPosition = player.Character.PrimaryPart.CFrame:GetComponents()
Profile.Data.Position = PlayerPosition
if not Profile then return end
Profile:Release()
end)
It’s because the character of the player has already been destroyed when PlayerRemoving is fired. You gotta store the position of the player somewhere else, maybe in a loop inside of PlayerAdded?