String Manipulation for Admin Commands?

Hello,

I am trying to make some custom admin commands and I’m hoping to do it in such a way you can type sentences, but by using string manipulation it’d still be able to recognise the command.

For example, let’s say I had a “shoot” command.

I’d want to say either: ‘shoot at my friend bob123’, ‘aim at my friend bob123’, ‘aim at my supercalifragilisticexpialidocious person bob123’ or ‘shoot at my enemy bob123’ but would still get the words Shoot/Aim and his Username recognising it as a command and who to do it to.

I find it a lot more practical than using the normal string.sub of ‘shoot user’.

I’ve tried looking around with string.match/gmatch and string.find but I’m not entirely sure how they’d go hand in hand on making this. If someone could guide me in the right direction or give me an example that’d be great.

Thanks in advanced.

This is a pretty difficult thing to do - lots of money goes into creating this sort of functionality in programs like Google Assistant or Siri (well, try to in the latter case)

I can really see the best way of doing this (whilst cheating) either with regex (if you want to follow the “shoot at my XXXX XXXX bob123” format), or by manually checking each argument after “shoot” using string.split until you find a complete username.

That’s what I was considering on doing, and it seems like it’s the best method. Only problem is I’d have to loop through each word, meaning it may or may not return 2 players if 2 usernames are found.

An example being:

“shoot at bob123 please mate”.
2 Player in-game named bob123 and pleasestop123.
It’d detect both when in hindsight I only wanted the one person.

Well, if you break when you find a full match, it’ll stop matching:

For every word in a message:

  • Check for command word (shoot)
    • For every word after the command word:
      • Is the word a top level child of Players?
        • If yes, break and run shoot on the user, break back into to top loop
        • If no, continue loop
4 Likes

That is not something you can avoid using the method you are looking to use. If that happens, I’d simply recommend having a popup that says “2 usernames detected; Please click the one you targeted”

It can be avoided if the flow algorithm I provided is used.

Alright, that seems like a good way to go about it. I appreciate your help man probably saved me a lot of time.

1 Like

“shoot at theshooter realusername” Would register the wrong username because it is not necessarily the first username that is the target.

Depending on what method you picked - if you decided to do exact matches, it’d make it a lot less likely that this occurs. Likewise, if you ignored the first 2 words after (i.e. with a regex crafted statement) this would overcome the issue at the sacrifice at flexibility.

Yeah of course. I was just mentioning this as a one in a million scenario. The chances of this happening are very slim but it would be good to implement a system that gives the player a popup in this unlikely scenario.

Alternatively, you can use string.split() to split the string into an array.
Eg. if I said ‘shoot at my friend username’

local array = string.split(message, " ") --Splits the message at every space
print(array[5]) --prints the username said

This wouldn’t work at all with what @ABritishChap is trying to do. The reason he asked is because username will not be in a specific order of the table.

Loop through the table and check if it’s a member of Players perhaps.

Yeah that has already been suggested earlier, please try to check through earlier replies to avoid sending duplicate suggestions.

Only reason I ask, is because I’ve seen it done before. But it must of took a lot of coding to get right. I think I have a gif or something of it somewhere. I’ll go look for it.

I’m going to try as many methods as possible to try and get it close or even spot on.

I think looping through each chat message to find the command keyword and a valid username is your best bet. There’s very minimal chance that you’ll encounter issues like 2+ usernames.