Using string.split for admin commands

Welcome to my tutorial, so 1 year ago I saw this function called string.split, it can be used for admin commands but I’ve seen alot of people having trouble with it so I made a tutorial about it, ok now let’s finally start the tutorial.

Why use string.split over string.sub?
it’s your choice, you will find string.split easier to work with than string.sub

Now let’s write an admin command system. A simple one

game.Players.PlayerAdded:Connect(function(plr)
plr.Chatted:Connect(function(msg)
 
end)
end)

Now, we will write a string.split variable.

game.Players.PlayerAdded:Connect(function(plr)
plr.Chatted:Connect(function(msg)
 local split = string.split(msg," ")
end)
end)

Now, we want to make a kick command

game.Players.PlayerAdded:Connect(function(plr)
plr.Chatted:Connect(function(msg)
 local split = string.split(msg," ")
 if split[1]:lower() == "/kick" then

 end
end)
end)

It still does nothing, let’s make it do something, but let me explain first, string.split returns a table of the splitted strings, just to not get confused.

Now, we will kick the player with the second argument.

game.Players.PlayerAdded:Connect(function(plr)
plr.Chatted:Connect(function(msg)
 local split = string.split(msg," ")
 if split[1]:lower() == "/kick" then
  if game.Players:FindFirstChild(split[2]) ~= nil then
   game.Players:FindFirstChild(split[2]):Kick()
   end
  end
end)
end)

Yes, it does work and kicks the player, but what if you want the 3rd argument to be the reason message? This is a tricky one, as you can’t simply just do split[3], if you do it will only capture one word from the sentence, so you would want to get all of the words after the two arguments, but how? We can make a new variable with an empty table, and after the 2 arguments, we will capture all words after the 2 arguments and assign the variable to all of the captured words after the two arguments, here’s how I do it.

local sentencetable = {}
local sentence = ""
sentencetable = split
for i = 1,2 do -- replace the 2 with how much arguments you have
table.remove(sentencetable,i)
end
-- make it to string
for i,v in ipairs(sentencetable) do
sentence = sentence..v
end

the finished code :

game.Players.PlayerAdded:Connect(function(plr)
plr.Chatted:Connect(function(msg)
 local split = string.split(msg," ")
 if split[1]:lower() == "/kick" then
  if game.Players:FindFirstChild(split[2]) ~= nil then
   local sentencetable = {}
   local sentence = ""
   sentencetable = split
    for i = 1,2 do -- replace the 2 with how much 
    arguments you have
   table.remove(sentencetable,i)
   end
   -- make it to string
   for i,v in ipairs(sentencetable) do
   sentence = sentence..v
   end
   game.Players:FindFirstChild(split[2]):Kick(sentence)
   end
  end
end)
end)

However, here’s a condition:
If you want a sentence argument, it must be the last argument and only 1 sentence, unless you change the split keyword

Any feedback is appreciated.

4 Likes

I have feedback: Why making an event like that? I see alot of people doing that.

Thank you so mush for this I know string.sub but not split I think I can use this for my math npc and to make own admin commands :slight_smile:

What do you mean “by event”?

For an example:

game.Players.PlayerAdded:Connect(function(plr)
plr.Chatted:Connect(function(msg)
 local split = string.split(msg," ")
 if split[1]:lower() == "/kick" then
  if game.Players:FindFirstChild(split[2]) ~= nil then
   local sentencetable = {}
   local sentence = ""
   sentencetable = split
    for i = 1,2 do -- replace the 2 with how much 
    arguments you have
   table.remove(sentencetable,i)
   end
event:Connect(function()
    print("Hi")
end)

This is just another way of connecting a function to an event. It’s the same as:

function DoSomething()
    print("Hi")
end

event:Connect(DoSomething())

Oh okay then, so that event uses less lines???

Yeah, it’s just preferences. Something you can do is:

Lets say we are creating a voting system. We want players to touch a part in order to vote:

local pad1Touched = workspace.VotePad1.Touched:Connect(function(hit)
    print("Someone has touched the votepad")
end)

Now, we don’t want this connection to stay forever, which is why we will Disconnect it after some time. Disconnecting an event is in simple words stopping whatever you connected to an event. Because we made the variable pad1Touched which is a RBXScriptSignal we can just do the following:

pad1Touched:Disconnect()

I am pretty sure you can do this with the other way of writing it as well, so it all just comes to preferences of how you want to write your code.

Why not just do sentence ..= v?

Also, I suggest that you indent your code with tabs.

1 Like

Wow, Lua is becoming much interesting, never knew this

I’ll do that next tutorial, thanks for suggesting

That’s actually a Luau thing only. Normal Lua doesn’t have this!

Luau has +=, -=, *=, /=, …= and maybe some others.

yep I never used it because usually I’ll mess with strings in more performance heavy way which would require some optimization and …= doesn’t really go with that

1 Like

ik they have all of this, but this?

..=

also, thanks for this info

Just questioning what the point of this tutorial when Roblox has one that is less complicated.

https://developer.roblox.com/en-us/api-reference/function/Player/Kick

Plus, it looks like your command allows anyone to kick anyone.

2 Likes

it has a “reason” argument which the kick command dosent have

its just some example code, not a full code to use.

2 Likes