PlayerRemoving issues

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
NVIDIA_Share_TppebVqedP

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)
2 Likes

You did not include line 51. I cant help you until it is included.

Ah sorry forgot to mention, i deleted some useless functions so line 51 is now line 44 *

Alright one second. I will inspect the line.

Try this:

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)

What it will do is it will repeat waiting until the variable PlayerPosition is not nil.


Nope, just threw this again.

Did you remove the comment? Im just trying to find the correct line.

No i did not, imjustgonnatypefor30charlimit

Alright one second. charrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr

Does this by any chance work?

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?

Hmm ok ill try this method and see if it works

He has you man, makes me feel dumb now that I understand.

works, thank you i feel dumb :sob:

I feel you, i tried the exact same thing a couple weeks ago.

Well, i’ve seem to ran into another problem
NVIDIA_Share_46kv11Zgjq
but for some reason the table is only this? unrelated but do you know why

nvm im dumb forgot to wrap it in brackets

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.