RemoveAsync not working? "Argument 1 missing or nil"

I’m not that good with datastores but I made a script that bans people when an admin enters their username and a reason and when the admin clicks the ban button, the player gets banned. The ban part of the script works but what doesn’t work is the unban part. I tried using RemoveAsync(), but it keeps giving me the error message “argument 1 missing or nil”. Heres the script:

game.ReplicatedStorage.Ban.OnServerEvent:Connect(function(player, rsn, plr)
	local success, errormessage = pcall(function()
		datastore:SetAsync(plr.UserId, true)
	end)
	
	if success then
		print(plr.." is now banned!")
	end
	
	plr:Kick(rsn)
end)

game.Players.PlayerAdded:Connect(function(player)
	local id = player.UserId
	
	local banned
	local success, errormessage = pcall(function()
		banned = datastore:GetAsync(id)
	end)
	
	if banned == true then
		player:Kick("You're banned from the game!")
	end
end)

game.ReplicatedStorage.UnBan.OnServerEvent:Connect(function(player, plr)
		datastore:RemoveAsync(plr.UserId, false)
		print(plr.." is now unbanned!")
end)

RemoveAsync only needs 1 argument, which is the key of the datastore.

Although, I can’t pinpoint where that error is. It is not in RemoveAsync.

is the key of the datastore the player’s id? I don’t know much about datastores…

A data store key can be anything as long as it’s a string and below 50 characters

Well, let’s see how I explain this.

When you do SetAsync, and the key doesn’t exist, it will create a key with the value. A Datastore is like a table, where there is a key and a value. You can think of it like this:

DatastoreName = {
    ["Player_1"] = Value,
    ["Player_2"] = Value,
    ["Player_3"] = Value,
}

Now, RemoveAsync removes the key, which in return also removes the value. So if you use RemoveAsync the datastore will now look like this:

Datastore:RemoveAsync("Player_2")

Result:

DatastoreName = {
    ["Player_1"] = Value,
    ["Player_3"] = Value,
}
2 Likes

Can there be more than 1 key??

Yes! As long as the key’s name doesn’t exist in the Datastore, it will create one.

You can’t have duplicate keys in a Datastore.

so does that mean the player’s id and the value true are the keys?

No, they are different.

The Player’s ID is the key, and the boolean, which is true, is the value.

This is what SetAsync looks like:

DatastoreName = {
    ["Player_1"] = Value,
    ["Player_2"] = Value,
}

DatastoreName:SetAsync("Player_3", Value)

Result:

DatastoreName = {
    ["Player_1"] = Value,
    ["Player_2"] = Value,
    ["Player_3"] = Value
}
1 Like

Ohhh, but do you think the player id is the key since It is the id that the datastore checks to see if the player has been banned?

Well, it is the key. SetAsync requires two arguments: the 1st argument is the key while the 2nd argument is the value.

Datastore:SetAsync(Key, Value)

Oh wait… I just realized something. You can’t get the player’s id from a textbox username right? So I have to use GetUserIdFromNameAsync?