Help with removeAsync

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? I want to remove keys from a dataStore

  2. What is the issue? The keys are not being removed

  3. What solutions have you tried so far? I’ve tried getting rid of the pcalls to see if there are any errors.

I’m trying to get rid of keys from a data store, but it won’t work. Here is the script that sets them:

local DataStoreService = game:GetService("DataStoreService")
local passStore = DataStoreService:GetDataStore("PassStore")

local Players = game:GetService("Players")

Players.PlayerRemoving:Connect(function(plr)
	print("plr removing")
	local statsFolder = plr:WaitForChild("StatsFolder")
	local pass = statsFolder and statsFolder:FindFirstChild("Pass")
	
	if pass.Value == true then
		local success, errorMessage = pcall(function()
			passStore:SetAsync(plr.UserId, "passed")
		end)
		if not success then 
			warn(errorMessage)
		end
		if success then
			print(tostring(plr.UserId))
		end
	end
end)

Here is the script that removes them:

local commands = {}

local prefix = "/"

local DataStoreService = game:GetService("DataStoreService")
local passStore = DataStoreService:GetDataStore("PassStore")

commands.remove = function(sender,arguments)
	if sender:GetRankInGroup(11635716) >= 254 then
		local userId = tonumber(arguments[1])
		local success, errorMessage = pcall(function()
			passStore:RemoveAsync(userId)
		end)
		if not success then
			warn(errorMessage)
		end
	end
end

game.Players.PlayerAdded:Connect(function(player)
	player.Chatted:Connect(function(message,recipient)
		local splitString = message:split(" ")
		local slashCommand = splitString[1]
		local command = slashCommand:split(prefix)
		local commandName = command[2]
		print(commandName)

		if commands[commandName] then
			local arguments = {}
			for i = 2, #splitString, 1 do
			table.insert(arguments,splitString[i])
			end
			commands[commandName] (player,arguments)
		end
	end)
end)

This could be a problem with the pass boolean value from the first script because if it is true when I leave, it will add my userId to the store again, but it defaults to false, and doesn’t save when it has been changed to true, so I don’t think that’s the issue. How can I fix this?

So, I have tested your second script and that’s all good and working. Now with your first script I wasn’t able to replicate what is going on in your game, but I do know the error is there. And if you are testing this on yourself, it’s not going to work because when you leave you just save your data again. The first script might work perfectly well, you will just need to test it on someone else.

2 Likes

Oh, so the whole issue might just be that I’m testing it on myself in studio? Does that mean it just won’t work in studio, but it will be fine when I publish the game?

Well not exactly. If you test it on yourself in a real server, you’ll run into the same issue of the data just saving as “passing” when you leave.

My way of testing was to run a test server where you spawn Player1 and Player2. I then went into play mode as myself and did /remove -2 (that’s player’s 2 userId) and hit stop. Then I used a data store plugin that allows you to look at player’s saves. And player2 no longer had any data, which signified the remove command working

1 Like

Oh, what plugin did you use? Is it the 300 robux dataStore editor?

Yeah it’s the one by sleitnick

1 Like