I have been recently coding a kick command (using a reference from the Devforum) but when I try kicking a player it breaks for attempting to index a nil value. I do not know exactly how to state my question for this except that it is not working. So I kind of expect this to be marked duplicate because it's probably been asked before, but I cannot find it.
Commands.kick = function(Sender, Arguments)
local Message = table.concat(Arguments, " ")
if (Arguments[1] == nil) then return end
local userPlayer = tostring(Arguments[1]);
local reason = tostring(Arguments[2]);
if (Arguments[2] == nil) then
reason = "No reason provided."
end
Players:FindFirstChild(userPlayer):Kick(reason)
end
The error is on line 24 (or line 9 for this snippet.)
ServerScriptService.Script:24: attempt to index a nil value

3 comments
then or either userPlayer is nil or reason is nil
Doesn't answer my question on how it's nil and how to solve it
Thanks for accepting