Nil value using string:split()

I’ve received a ServerScriptService.Script:9: attempt to call a nil value in my repot command:

...
game.Players.PlayerAdded:Connect(function(reportingPlayer)
	reportingPlayer.Chatted:Connect(function(msg, recipient)
		msg = string.lower(msg)
		local arguments = msg:Split(" ")
		local command = arguments[1]
		local subject = arguments[2]
		local reason = arguments[3]
		local reportedPlayer = Players:FindFirstChild(subject)
		local PlrName = reportedPlayer.Name
...

The error is replicating on line 9, local arguments = ...

2 Likes

:Split(...) should be lowercased :split(...). This is how all functions from the standard library (string, math, table, etc) is written.

2 Likes

It hasn’t fixed my issue:

local Players = game:GetService("Players")
local PlayersChildren = Players:GetChildren()
local Http = game:GetService("HttpService")
local Webhook = ...

game.Players.PlayerAdded:Connect(function(reportingPlayer)
	reportingPlayer.Chatted:Connect(function(msg, recipient)
		msg = string.lower(msg)
		local arguments = msg:split(" ")
		local command = arguments[1]
		local subject = arguments[2]
		local reason = arguments[3]
		local reportedPlayer = Players:FindFirstChild(tostring(subject))
		if reportedPlayer then
			local PlrName = reportedPlayer.Name
			if string.sub(command, 1, 8) == "-report " and string.sub(msg, 9) == string.lower(PlrName) then
				local data = 
					{
						["contents"] = "",
						["username"] = reportingPlayer.Name .. " - "..reportingPlayer.UserId,
						["avatar_url"] = "https://www.roblox.com/Thumbs/Avatar.ashx?x=500&y=500&Format=Png&userId="..reportingPlayer.UserId,
						["embeds"] = {{
							["title"]= reportingPlayer.Name.." has reported "..reportedPlayer.Name.." for exploiting in a server of "..#PlayersChildren.." players",
							["url"] = "https://www.roblox.com/users/profile/"..reportingPlayer.UserId,
							["description"] = "Reason - "..reason,
							["type"]= "rich",
						}}
					}
				print("reported "..reportedPlayer.Name)
				Http:PostAsync(Webhook,Http:JSONEncode(data))
			end
		else
			warn("Player not found")
		end
	end)
end)
1 Like

bruh legit just print the msg and see what it says.

1 Like

Are you trying to be funny or are you serious here

1 Like

if it prints out a nil error it means that it is nil so msg is nil and I don’t see you define it anywhere

1 Like

bruh the msg is literally defined here what are u talking about

1 Like

He defined it in Chatted’s event args

Do you still get the same error or did a new one show up? Since it’s impossible for it to still produce that error unless msg was nil, in which case the string.lower call would’ve errored

2 Likes

For this most recent script, nothing returns, no errors, nor does it send the webhook data to my channel.

1 Like

It shouldn’t return nil as I have just used the split method a few times in the chat to make admin commands in my next video which will be today at 12:00pm est btw if you want to see how I split it. I don’t know if it will help but if it still doesn’t work then maybe try it?

1 Like

Topic solved by Kiriot via Discord, thanks to all that helped

1 Like

What was the issue so people in the future can know how to solve it

1 Like

The issue was that the OP was checking if the return of msg:split(" ") contained a space (which is an impossible scenario), and also due to the string.lower part the code would fail to grab the player if they had an uppercase letter in their name. Also the reason part supported only 1 word.

Here is the fixed code:

local Players = game:GetService("Players")
local PlayersChildren = Players:GetChildren()
local Http = game:GetService("HttpService")
local Webhook = ...

game.Players.PlayerAdded:Connect(function(reportingPlayer)
    reportingPlayer.Chatted:Connect(function(msg, recipient)
        local arguments = msg:split(" ")
        if arguments[1] then
            local command = table.remove(arguments, 1)
            if command == "-report" then
                local subject = table.remove(arguments, 1)
                local reason = table.concat(arguments, " ")
                local reportedPlayer = Players:FindFirstChild(subject)
                if reportedPlayer then
                    local PlrName = reportedPlayer.Name
                    local data = 
                        {
                            ["contents"] = "",
                            ["username"] = reportingPlayer.Name .. " - "..reportingPlayer.UserId,
                            ["avatar_url"] = "https://www.roblox.com/Thumbs/Avatar.ashx?x=500&y=500&Format=Png&userId="..reportingPlayer.UserId,
                            ["embeds"] = {{
                                ["title"]= reportingPlayer.Name.." has reported "..reportedPlayer.Name.." for exploiting in a server of "..#PlayersChildren.." players",
                                ["url"] = "https://www.roblox.com/users/profile/"..reportingPlayer.UserId,
                                ["description"] = "Reason - "..reason,
                                ["type"]= "rich",
                            }}
                        }
                    print("reported "..reportedPlayer.Name)
                    Http:PostAsync(Webhook,Http:JSONEncode(data))
                else
                    warn("Player not found")
                end
            end
        end
    end)
end)
1 Like