How to send data from client to server when player is removed

I want to send local script data to the server when the player leaves, but when I try to use a remote event with the PlayerRemoving event it just doesn’t work, so I thought the remote event was destroyed before PlayerRemoving fired, but when I try to print it , it shows in output. Here are the scripts:

Local script


Server script:

local DSS = game:GetService("DataStoreService")
local DS = DSS:GetDataStore("DS")
game.ReplicatedStorage.RemoteEvent.OnServerEvent:Connect(function(plr, NewNumber)
	DS:SetAsync(plr.UserId.."Number", NewNumber)

And here is where they are:

Thanks in advance.

Why are you firing a remote event from the client to the server when the player leaves? Just connect a .PlayerRemoved() event on the server and do all the work you need there?

1 Like

Yeah, this needs to be handled by the server. Even if you could fire a remote when someone leaves, it wouldn’t work if their game crashed or they used alt F4.

Because i the value Number is in local script, and I want to save it in a data store when the player leaves

An exploiter could just change that value, leave the game and then it’ll save it for them. There’s absolutely no reason why you should trust the clients info, let alone save those values directly. All data-related things should be handled on the server.

Since the addition of player to remote events you can secure events better, some ammount of client to server communication is unavoidable, but I think its pointles saving when they leave as data should be saved regularly anyway

Yes, but can i do this ?
It’s just a question I have

I understand this, but how they’re approaching it seems to be rather inefficient.

When the player leaves local scripts remove themselves, as far as I know majority of the script after a player has left are removed within a milisecond.

So basically what your trying to acomplish is just a needless use for a script.

You could probably stall for time using require but without the client actual present it wouldnt run anyway
Hope this helps you understand why we are all saying its pointless to even try to do it