CharacterRemoving and PlayerRemoving functions work in a different sequence

Hello. When a player presses the stop button in roblox studio, the CharacterRemoving and PlayerRemoving functions work in a different sequence, I want to save the gun as a equiped tool and the number of its ammo . And sometimes CharacterRemoving is not called at all. How can I save the pistol and its ammo count if the character is no longer available in the PlayerRemoving function?


local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(player)
	player.CharacterRemoving:Connect(function(character)
		
	end)
end)

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

I’m lost, what do you mean by “I want to save the gun as a equiped tool and the number of its ammo”, do you want to save it when the player leaves, or the character died, or is it both?

1 Like

If you’re trying to save data, I’d recommend storing the data outside of the player or their character as either could be removed unexpectedly by the client. Also, if you leave the game in Studio using the esc menu and leave button, it should be more accurate. Although you may need the test mode type set to local server for it to work as expected, if it isn’t already set to that.

1 Like

And if a player in a real game and not in the studio closes the window by clicking on the cross instead of exiting through leaving? Will these two different actions give the same result?

To begin with, I want to make if the tool is in the character’s hand and / or inventory, when leaving the game, everything was saved in the database, I want to record everything in one table, but when the game is exited, the character is no longer available in PlayerRemoving. And I save all the data to the database just in this function.

Closing with the X in an actual game is the same as using the leave button as it’s only disconnecting the client. However, while I haven’t tested this, I think closing differently than through the leave button removes the player instance before the server’s end can trigger PlayerRemoving fast enough.

Anyway, before I write an essay, I’ll keep it short. Never store data that you want to save within the player instance as PlayerRemoving might not get to it in time. But when you close using the stop button in Studio, test mode is running both the server and client at the same time.

So pressing stop vs leave is like closing everything at once in no particular order rather than the “correct” order. This doesn’t apply when running the separate test mode that opens up multiple instances in test mode.

1 Like

I want to save in the database, the tools from the backpack and the tool that is in the character’s hand, but the problem is that I just checked in the studio and there when leaving through the leave, these functions do not always work in the same order. I have the data stored in the PlayerRemoving function to access the backpack. But how to save data from a character if PlayerRemoving can run ahead of CharacterRemoving ?

If all you want is to save when the player leave’s you don’t have to use CharacterRemoving, or are you actually using this to store the data?

Hmm, from what I understand when the game shut’s down, the player removing does not trigger because it’s not fast enough, what you need is BindToClose function because this trigger’s when press the shutdown button on the main page of your game, this also applies to the studio.

i use CharacterRemoving because that’s where the tool i want to keep is stored

Have you tried getting the tools in the character from the player removing function?
Because it still returns player

You don’t need CharacterRemoving if all your doing is save the tools when the player leaves, all you need is PlayerRemoving; CharacterRemoving function actives when the player dies/destroyed from the workspace.

It depends on the sequence in which characterRemoving and PlayerRemoving will work, some time one of the functions may work forward another time another function

I need to get the tool that is inside the character

You can still get the tools inside the character with PlayerRemoving, player.Character.

game:BindToClose(function() -- if the server/game is shuting down.
	for _, player in pairs(game.Players:GetPlayers()) do
		local character = player.Character or player.CharacterAdded:Wait()
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	local character = player.Character or player.CharacterAdded:Wait()
end)
1 Like

This won’t work the character is being removed instead of being added

What do you mean, I still don’t get what your main problem is and what you want to achieve.
From my understanding you wanted to save the pistol and ammo if the player left.

Like what I said you don’t need the CharacterRemoving function if all your doing is just save the data when the player leaves the game.

Just get the tool inside the character with this character:FindFirstChildOfClass("Tool"), then save it.