Deleting Player's Data by LEAVING the game

  1. What do you want to achieve? Keep it simple and clear!

I am trying to create mechanism similar to PD (Permanent Death), basically removing player data upon death, but I want to make it so if player leave the game, their player data would be deleted.

  1. What is the issue? Include screenshots / videos if possible!

Naturally, I scripted a basic script to do that, but it just don’t work for some reason, the script is provided below:

game.Players.PlayerRemoving:Connect(function(player)
	local dataStore = game:GetService("DataStoreService"):GetDataStore("LumiData")

	local key = tostring(player.UserId)
	local success, err = pcall(function()
		dataStore:RemoveAsync(key)
	end)
	if success then
		print("Player data removed successfully")
	else
		warn("Failed to remove player data: " .. err)
	end
end)
  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

I looked everywhere on Devforums, tried it but it doesn’t work, I am not sure why, my data is structured in simple way - its stored in “LumiData” and player’s data is referenced using UserID, with stats in it and everything.

A potential issue can be the way you access the key, you are using tostring().
Are you sure the key is a string and not just a number? Since you are using the UserId.

Also, does it print anything? If so, what does it print?

1 Like

Whoops. Yeah, it’s not string, and also it doesn’t print anything. What should I use instead of tostring() for number key?

You can directly use local key = player.UserId
Also, can you check if the pcall is being executed? Try adding a print statement inside there.

Small advice:
Move the dataStore variable outside the event, since it’s constant and will not change.

Thanks for small advice, and weirdly, I added print statement and changed PlayerRemoving to PlayerAdded as it would be easier to test, it does execute the pcall, but it doesn’t remove the data, I still have same stats in leaderstats.
edit: logs
image

If I understood correctly, if you rejoin the data is still there? Even tho you have called :RemoveAsync() beforehand?

If yes, then make sure the DataStore you use is the correct one, and that the key you access is the correct one.

Also, make sure to clear the leaderstats as well when calling :RemoveAsync()

1 Like

Yes, you are right, data is still there even if I rejoined.

Clearing the leaderstats beforing clearing the data seemed to have fixed the problem, and I also got warning “DataStore request was added to queue. If request queue fills, further requests will be dropped. Try sending fewer requests.Key = 1989818146” Is this important and something that I should worry about?

This happens when you call too many DataStore requests, you shouldn’t really worry about it. Unless when the log is full of it, then you need to change the way you handle data.

But I’m glad the issue is fixed.

1 Like

Alright, thanks so much, I greatly appreciated you helping me :slight_smile: and have a good day.

1 Like

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