[Help} How To Make Commands Like /help, /discord etc..?

I need help on how to make /help, /discord scripts for my FiveM server ASAP

1 Like
AddEventHandler("chatMessage", function(source, name, message)
	if message == "/discord" then
		TriggerClientEvent("chatMessage", source, "DISCORD", {204, 51, 255}, "PLACE YOUR DISCORD LINK HERE")
	end
end)
1 Like

@xander1998 is correct, however I would write it this way instead :

AddEventHandler("chatMessage", function(s, n, m)
	local message = string.lower(m) -- This prevents it from NOT recognizing /DISCORD as the command (just in case the user has caps lock on)
	if message == "/discord" then
		CancelEvent() -- Hide the message from user chat
		TriggerClientEvent("chatMessage", s, "DISCORD", {204, 51, 255}, "DISCORD LINK")
	end
	if message == "/help" then
		CancelEvent()
		TriggerClientEvent("chatMessage", s, "HELP", {204, 51, 255}, "CHANGE ME")
	end

end)

This will help users out, just in case they don’t type the EXACT caps needed for the command.
string.lower(m) converts the message to all lowercase when a message is received.

1 Like

sorry but where exactly do you put that ? in cl_chat.lua? im new to all this, thanks in advance anyone who can help :slight_smile:

I have been absent from the community for some time now, however if you have not yet found your answer.

The script shown above is the server_script. So you put it in your server script lua file.

Just a question, from where does the parameters (s,n,m) get it’s data from? Where are these functions/events defined?

Hey Xander sorry to ask you again lol but do you put the AddEventHandler text in RPChat right?

?
I don’t understand that question at all.

he is not talking about any resource called RPChat man

Lol my bad fellas I worded that wrong, my question was where do I put the text that was provided above?

You are reading a topic that june 17.

Find a more recent topic.

RegisterCommand('help', function(source, args, rawCommand)
          -- Do stuff in here
end, false)

The function has 3 different arguments that you can use.

  1. ‘source’ - This is the player that typed the command in chat.

  2. ‘args’ - This is what stores what the player says after the command. For example, if I did /help discord
    I could then get the word I said after the command which is ‘discord’ by using args[1]. The number 1
    being the first argument. You can do this multiple times, with multiple arguments i.e. args[1], args[2] and
    so on.

  3. ‘rawCommand’ - I’m genuinely not sure what this means or is used for, however in my experience it hasn’t
    really been necessary to know it.

Hopefully I helped at least a little bit! :slight_smile:

If you type in /help one two three from your example, then source is my source, as you know, and args is {'one','two','three'}, but rawCommand is help one two three
It’s quite literally the raw command from before it got chopped up into args.

Useful for knowing which command was issued when you have multiple ones hooked up to the same function, for example.

1 Like

Source, Name, Message and this is a Server.Lua part