Hello guys,
I am wondering if PlayerRemoving and BindToClose will be called and the following code will run. In studio, I can’t trigger the PlayerRemoving event.
Does these code work?
My main conern is that the folder will be nil due to the player character leaving the game first
playerService.PlayerRemoving:Connect(function(player)
local waitForCharacter = player.Character or player.CharacterAdded:Wait()
local playerTagsFolder = waitForCharacter:FindFirstChild("Tags")
end)
game:BindToClose(function()
for i,v in pairs(game.Players:GetPlayers()) do
local waitForCharacter = v.Character or player.CharacterAdded:Wait()
local playerTagsFolder = waitForCharacter:FindFirstChild("Tags")
end
end)
I have another function for auto saving player’s inventory data. Basically I want to have two systems in my game for extra protection. I don’t know whether that will be better. Auto saving works in the game but it is just the player removing event having some problem
My function for saving the player’s inventory data
local function savePlayerData(player)
local waitForCharacter = player.Character or player.CharacterAdded:Wait()
local savedTags = {}
local playerTagsFolder = player:FindFirstChild("Tags")
local playerTagsFolderChidren = playerTagsFolder:GetChildren()
if playerTagsFolder and playerTagsFolderChidren then
for _,items in pairs(playerTagsFolderChidren) do
table.insert(savedTags, items.Name)
print("Adding "..items.Name.." to "..player.Name.."'s Inventory")
end
local success = pcall(function()
TagDataStore:SetAsync(player.UserId, savedTags)
end)
if success then
print("Successfully saved "..player.Name.."'s Inventory data")
else
warn("Couldn't save "..player.Name.."'s Inventory data")
end
else
warn("No Folder")
end
end