Chat Command Issues

I’m trying to make a chat command. Basically when an admin chats ?rank user role
he can write whatever he want in role and change the role text to whatever he wants and the issue is that i want to get the username and role from that chat / string. But how would i do that?

To extract the username and role from the chat string, you can use string manipulation functions in Lua. Here’s an example of how you can do this:

local chat = "?rank username role" -- replace this with the actual chat string
local command, username, role = chat:match("^(%S+)%s+(%S+)%s+(.+)$")

if command == "?rank" then
    -- handle the rank command
    print("Username:", username)
    print("Role:", role)
    -- You can use these values to update the user's role as needed
    -- handle the case where the chat string does not match the rank command
    print("Invalid command")

In this example, we use the match function to match the pattern of the chat string. The pattern is defined as ^(%S+)%s+(%S+)%s+(.+)$, which matches three groups of characters separated by spaces. The first group is the command ?rank, the second group is the username, and the third group is the role.

We then check if the command matches ?rank and handle the command accordingly. You can replace the print statements with your own code to update the user’s role based on the extracted values.

Note that this is just one way to extract the username and role from the chat string, and there may be other patterns that you need to handle depending on your specific use case.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.