Converting comand to function

I need to convert comand (comand parametrs) to function for example kick robloxian52656559889486856666854(i dont know if this plyer exist but if yes dont understand it as something bat its just example username) to function kick with parameter robloxian52656559889486856666854

you could do something like

function Kick(player)
game:GetService(‘Players’):FindFirstChild(player):Kick(“You have been kicked from the game.”)
end

for example then you could do
Kick(‘robloxian52656559889486856666854’) and it would kick them

yes but my question as at diferent point: not how to kick player, but kow to convert kick comand to kick function

you could use a chathook to detect whenever the player says “kick player123” and then have your code run the kick stuff

YEEES put how to convert kick comand to kick function ban comand to ban function

You could’ve explained this better but I’ve seen the other post so I understand. A simple way to do this is have a dictionary of functions and then call the function identified in the table as first word they say after the prefix. I can show you how this is done in code tomorrow but it’s 4am for me right now.

A dictionary is basically a table with identifiers replacing number indexes

E.g.

{player = “bad”}

  1. am not so stupid (i am 6 years working with c# and there dictionary is) but how to make the first word will set function and every next parametr

You may want to look at the Chat Modules page, it contains a guide that can teach you how to make commands.

Also gonna give a blunt blow right here: C# is not Lua, 6 years of C# doesn’t mean much when translated into years of experience in Lua :eyes:

1 Like

Considering the questions you are asking and how you are asking them I basically treated this like you have 0 knowledge of scripting. I’m trying to help theres no reason to act defensive.

i am not defensing i am only saying the reason for my question is my english

Typical chat command scripts usually parse out the command then test it against every command they have. They fire functions as they detect commands.

The example provided below isn’t a practical solution and only serves the purpose of showing an example of how something like this might work. Do not use it for practice.

For example:

--Example of a function that would fire from a command
function kick(player)
game.Players:FindFirstChild(player):Kick(“You have been kicked from the game.”)
end


---Imagine you have everything parsed and you have the raw command string as the variable "command" and you have any arguments saved as a table

if command == "kick" then
   if arguments[1] then --I parse my arguments as tables so I can access each argument individually 
     kick(arguments[1]) --This will fire the function above 
   end
end

If you need help parsing strings, check out this article to learn about the various string functions.

A very good example of all of this is found in the Chat Modules page as @NicholasY4815 has said. It walks you through creating your own commands. It is specifically designed for the Lua chat system in Roblox, meaning it might be more suited for you.

That’s a terrible idea because your code becomes repetitive and hard to edit. You can literally just add your commands to a dictionary and check if a function exists before calling it then pass in other parsed arguments as a tuple. This system won’t scale up your script when you have to add a command and allows you to make your system modular.

yes but i am sending is with remote event so how to split it
and isnt here something like object oriented programing to do it beter

There is no reason to send remote events. Have a listener on both the server and the client if you’re going to mix commands. Both will be secure as long as you check the user IDs or group and the code on the client will not be able to do something exploits can’t already do.

I understand that and I didn’t actually mean for that to be practical. I simply meant in pseudo code, this is how it basically works.

The real solution would be in the Chat Modules page that was linked - that’s a much more practical solution

*Edited my original post to correct that.

1 Like

You could use ModuleScripts if you want a more OOP approach, I don’t find it much of a difference since it’s not much different from using a dictionary.

You could have a separate module for commands that returns a dictionary or iterates through a folder of modules and combine them to create a dictionary that can be used to look up commands. This wouldn’t be object orientated but theres no real reason for it to be set up that way.

and can i execute the function without ifs for every comand

yes you are true using modules scripts is only moving the code from main script to library and is useful only when i need the function in many scripts

Yes

   commands.kill = function(information, playerToKill)
    -- do stuff
   end

   --- words = table of parsed words from user chat message
   if commands[words[1]] then
       table.remove(words, 1)
       commands[words[1](information, unpack(words))
   end
2 Likes