Ban Command Userid

Hello I am trying to make a ban command that searchs if the username is valid then does something. I have it working but the problem is when it can’t find a user I wan’t it to fire an event, this does not work. It says: “Players:GetUserIdFromNameAsync() failed: Unknown user” which is right but when this happens I want something to fire, code:

local targetID = players:GetUserIdFromNameAsync(target)
			
			if targetID == nil then
				print("No User Found")
				remoteServer:FireClient()
			else
				print("No User Found")
			end

Use a protected call to prevent the script from aborting if the function throws an error.

local success, targetID = pcall(players.GetUserIdFromNameAsync, players, target)

if success then
	print("User found")
else
	print("No User Found")
	remoteServer:FireClient()
end
1 Like

While pcall() will handle this issue it’d be better if you were to instead validate the value of the variable named “target” before attempting to use it, i.e;

if type(target) == "number"

You can use the “type()” global to ascertain the variable’s data type in order to verify that it is indeed a number.