Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Kick Command breaking due to a nil value?

Asked by 5 years ago

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.

01Commands.kick = function(Sender, Arguments)
02    local Message = table.concat(Arguments, " ")
03    if (Arguments[1] == nil) then return end
04    local userPlayer = tostring(Arguments[1]);
05    local reason = tostring(Arguments[2]);
06    if (Arguments[2] == nil) then
07        reason = "No reason provided."
08    end
09    Players:FindFirstChild(userPlayer):Kick(reason)
10end

The error is on line 24 (or line 9 for this snippet.)

ServerScriptService.Script:24: attempt to index a nil value

0
then or either userPlayer is nil or reason is nil EternalScythe 222 — 5y
0
Doesn't answer my question on *how* it's nil and how to solve it Warriorfoox 25 — 5y
0
Thanks for accepting Mr_Unlucky 1085 — 5y

1 answer

Log in to vote
0
Answered by
Mr_Unlucky 1085 Moderation Voter
5 years ago
Edited 5 years ago

The issue was that since you're using the admin tutorial here, it has this line in the "ParseMessage" function. What is does is that it uses string.lower to the message, causing the entire thing to become lowercase. Because of this, the kick command processed the usernames as lowercase therefore not finding any players.

01local Commands = {}
02local Prefix = "!"
03 
04local function ParseMessage(Player,Message)
05    local PrefixMatch = string.match(Message,"^"..Prefix)
06 
07    if PrefixMatch then
08        Message = string.gsub(Message,PrefixMatch,"",1)
09        local Arguments = {}
10 
11        for Argument in string.gmatch(Message,"[^%s]+") do
12            table.insert(Arguments,Argument)
13            print(Argument)
14        end
15 
View all 47 lines...

I modified the code from the tutorial a little bit. Make sure to accept my answer if its right!

Ad

Answer this question