How to pull 3rd arguement from string.sub?

You need to do this:

msg:sub(4,string.len(msg))

This will go from the 4th position, all the way to the end of the string.

Ohh, so use:

splits[3]
--gets the third table index for string.split() or string:split() which would be the reason

You already declared splits, but you’ll want something like :Kick(table.concat(splits, " ", 3). The last 2 arguments of table.concat are where to start and where to stop concatenating, since the first value is the command, and the second one is the player name, and then everything else would be the message. So you just undo the splitting of the message. The fourth argument would by default be the length of the table. The third argument by default is 1, i.e. the beginning of the table as well.

No, I’m trying to get the 3rd arguement.

1st arguement, 2nd, 3rd

such as

!k funnyfunns (3RD ARG)

Not workinggggggggggggggggg. I already tried splits[3]

Did you read my post? I didn’t do splits[3], I did table.concat(splits, " ", 3)

I just tried it!!! Dosent work I cri

--- How to make ADVANCED commands.
game:GetService("Players").PlayerAdded:Connect(function(plr)
	plr.Chatted:Connect(function(msg)
		if msg:sub(1,3) == "!k " then
			local splits = string.split(msg, " ")
			local plr2kick = game.Players:FindFirstChild(msg:sub(4))
			local rsn = ""
			if plr2kick then
				plr2kick:Kick(table.concat(splits, " ", 3))
			end
		end
	end)
end)

Like this?:

local reason = nil

if (split[3]) then
    reason = splits[3]
end

Player:Kick(reason)

Well, there’s no real use for the if statement, you can leave it out and do:

local reason = splits[3]

Player:Kick(reason)

Not working! I cri now :((((( it dont work

Btw its a server script! 1111111

Yeah:

local plr2kick = game.Players:FindFirstChild(msg:sub(4))

Yeahhh:

        if msg:sub(1,3) == "!k " then
			local splits = string.split(msg, " ")
			local plr2kick = game.Players:FindFirstChild(splits[2])
			local rsn = splits[3]
			if plr2kick then
				plr2kick:Kick(rsn)
			end
		end

??? what?? do u mean

--- How to make ADVANCED commands.
game:GetService("Players").PlayerAdded:Connect(function(plr)
	plr.Chatted:Connect(function(msg)
		if msg:sub(1,3) == "!k " then
			local splits = string.split(msg, " ")
			local plr2kick = game.Players:FindFirstChild(msg:sub(4))
			local reason = nil
			
			reason = splits[3]
			
			if plr2kick then
				plr2kick:Kick(reason)
			end
		end
	end)
end)

That doesn’t work because it’s trying to get everything after the fourth character, try splitting by a space, check if the first argment is a valid command, and if the second is a player.

Works on my machine:

local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(player: Player)
	player.Chatted:Connect(function(message: string)
		local contents = message:split(" ")
		
		if contents[1] == "!k" then
			local target = Players:FindFirstChild(contents[2])
			
			if target then
				target:Kick(table.concat(contents, " ", 3))
			end
		end
	end)
end)

I mean test the code block I provided you with, lol.

And for the “reason” aspect of the code, please listen to @sjr04

Yay!!! Thank uuuuu u and incapaz

but how to get rest of string?

Here, look at the code block where incapaz kicked the player.

Okay, thank you and incapaz! U guys have great day :smiley: