profile:ListenToRelease(function() --Does the function when Release() is called
ProfileManagerModule.Profiles[player] = nil --remove the players profile from Profile Manager
print("player released")
player:Kick("Don't worry! The player's data has been saved. The player had to be kicked out to prevent data corruption")
end)
It should print “player released”, but it doesn’t in game. Yes I did publish it. It’s a server script. Yes it is Enabled.
I left the server, but the PlayerRemoving said it launched, but it didn’t Release() the player. When I rejoined the game, it ERRORED.
game:GetService("Players").PlayerRemoving:Connect(function(plr)
print("player removing launched")
SpawnBackpack(plr:FindFirstChild("Inventory"), plr, plr.Character or plr.CharacterAdded:Wait())
local profile = ProfileManagerModule.Profiles[plr]
if not profile then return end
profile:Release()
end)
This doesn’t seem like a problem with ListenToRelease; instead it seems like SpawnBackpack is yielding indefinitely. Try commenting out the SpawnBackpack function as a test and see if the profile releases. Also, this code snippet will cause the code to hang forever if the player does not have a character:
plr.Character or plr.CharacterAdded:Wait()
Since it’s not immediately obvious what happens during a player’s death, I would add an if statement that checks if the player has a character before calling the SpawnBackpack function:
local character = plr.Character
if character then
SpawnBackpack(plr:FindFirstChild("Inventory"), plr, character)
end
If the code still doesn’t run, then add print statements inside SpawnBackpack to narrow in on exactly where the code stops running. Additionally, print out the contents of profile directly before releasing just to be sure.
Going Further
Instead of only keeping information for the player’s inventory inside the player’s character, keep data in a table on the server that is added to and removed from as soon as a player adds/removes something from their inventory. This way the items in the player’s character are only physical representations of what the table says. This also allows for easier to manage data pertaining to those items such as timestamps/dataIds.
The issue was definitely the yield, because when I commented it out. The Release() did work. I don’t store any items inside the character, only the player. I don’t even save the items in datastore, only the Coins value (the data stored in a module script, then saved using ProfileService). I will try to see if it prints character (inside of the if character then) to see if the SpawnBackpack() runs and finds character. Thank you for helping me with the Release() issue
game:GetService("Players").PlayerRemoving:Connect(function(plr)
local character = plr.Character
if character then
SpawnBackpack(plr:FindFirstChild("Inventory"), plr, character)
print(character) -- It should print this, but it doesnt like seen in the video
end
local profile = ProfileManagerModule.Profiles[plr]
if not profile then return end
profile:Release()
end)
The only print() that works is inside of ListenToRelease()