[HELP] The /roll script, or how to random math on gmod

Hello everybody !
First of all, sorry for the potential mistakes and sorry for posting this thread, i know there were a few of them about the same subject, but advices were given, and i’m french ( studying english ) that’s why i have some issues with it, i don’t understand evrtything each time, so it is very hard for me to make the script work.

( ill try to explan what i want : )
I do have a DarkRP server, and i saw on a another i was playing on that a command exists, where you just have to write " /roll" in the chat, and it gives you ( randomly ) a number between 1 and 100 ( both included ), which will define if you won or not the rp action !

Here is a quick example :
A gangster aims a man with a gun, and says " GIVE ME YOUR MONEY !"
the man can try a /roll :

The man : /me try to catch the gun
the man : /roll
The man tried a roll and obtained randomly 64 on 100
The gangster : /me tries to resist
The gangster : /roll
The man tried a roll and obtained randomy 41 on 100 <------- less than the other, so he loses the RP action and has to drop the gun

Can you please tell me what is the script or command to make to do this very cool thing that could be useful for RP ?
I really need your help on this, i’ve been searching for a while and didn’t understand because of the difficult terms, thanks a lot to eveybody,

   Garrysmodly,  Billy.

You can try and keep searching for the addon, or you can try and code your own, It wouldn’t be hard to code one yourself. Heres two tutorials on chat commands and the gmod wiki is a very useful tool.

I wrote 3 “tutorials” on chat-commands. Each file has several examples with the first file being the most practical and the other 2 showing alternatives. The first file shows how to turn ! and / chat commands into console commands and has another example so that instead of using concommand.Add for the chat-command you use chatcommand.Add ( in the same was as concommand.Add ) except that it’ll add salt and pepper ( prefix and postfix ) so /kick x won’t call the console command kick which can’t be changed without a dll but will call your custom version…




There are also alternative methods ( I may have included it ) to have the chat-command on the client ( OnPlayerChat or so ) instead of the server ( PlayerSay ), but take a look. Remove .html to view .lua

Thank you guys I really appreciate your help !
These tutorials help me a lot to understand the way lua works, so, it’s very nice !
Oh, by the way, I have a little question, if I do create the script, should I put it in addons for it to work , like any other script I buy ?
Thanks again ! :downs:

You should put the script in lua/autorun for it to be run, well, automatically. If you put it in the server/client folders in lua/autorun, it will be run on either the server or the client instead of being run shared like it is in normal lua/autorun. All the addon folder does is mount the contents to the /garrysmod/ directory.

Or, You could create it into an addon for your server, but you would just need to create the right folder set up, you can look at the other addons that you have and just try and copy a basic addon folder set-up, all you would need to contain the files for it would be, addon/youraddonname/lua/autorun/ the files would be kept in the autorun, make a server side file “init.lua” and a client side “cl_init.lua”. the cl_init.lua will contain all the clientside information, and the init.lua will contain all the serverside information, once you have that you, would need to add the line


, and you can also search on google, and probably find a better tutorial, and information.

While that would work, it seems like a whole lot of trouble to be able to get the same overall outcome, being that your script is run shared.

Do not do this…

Actually, if everyone used those names inside of lua/autorun/ or *client/ or server/ then only ONE addon would run… All of the folders are merged together into a virtual directory structure and files named the same are overwritten or ignored if one exists which is why darkrpmodification addon works with overwriting core-files on DarkRP, and it’s why you can create an addon/lua/entities/weapon_tttbase/ and overwrite ttt weapons while allowing your base game-mode directory to remain untouched ( so it can be updated without losing content, and then you can compare changes and update your addon if it does replace a core file )…

Also, you do NOT need to use include or AddCSLuaFile in lua/autorun/, lua/autorun/client/ or lua/autorun/server/ folders. If you want to create a structure outside of those 3 folders then you will need to add the files OUTSIDE of the structure… Also, the order it loads is SHARED, ie autorun/, then CLIENT/SERVER…

Thank you all, you really try to help me, and i do appreciate that !
It’s quita hard to begin, even with lua which is said pretty simple.

But, it still doesn’t work, i’ve done this ( the folder ROLL is in autorun, and the file is called cl_roll.lua ), here’s the code you gave me :

hook.Add( “PlayerSay”, “Acecool:ChatCommands”, function( _p, _msg, _team )

	local _bSlash = string.StartWith( _msg, "/" );
	local _bExclaim = string.StartWith( _msg, "!" );

	if ( _bSlash || _bExclaim ) then
		_p:ConCommand( string.sub( _msg, 2 ) );

		if ( !_bExclaim ) then
			return "";
end );

Thank you for taking the time to help me with that, you guys are amazing :slight_smile:

And are you creating the roll command? Also, use [noparse][lua][/lua][/noparse] tags.

What I meant by that statement is that, for this particular script (a chat command), you can use a unique file name and put it in autorun. It is unnecessary to include or AddCSLuaFile an extra file for such a simple script. I’m well aware that you don’t put ALL scripts in autorun, but for a single chat command script, putting it in autorun would work fine.

[lua]concommand.Add( “rollthedice”, function( ply )
PrintMessage( HUD_PRINTTALK, ply:Nick()…" has rolled a “…math.random(1,100)…”.")
end )

function RollCommand( pl, text, teamonly )
if text == “/roll” then
pl:ConCommand( “rollthedice”)
return “”
hook.Add( “PlayerSay”, “Chat”, RollCommand )

Just put this in lua/autorun and it will work. I just tested it for myself.

Thanks a lot ! But, may be my bad, it doesnt work on my server, when y do a /roll, it’s written this in my consol :
[lua] ERROR lua/autorun/lerolljenkins.lua:2: attempt to call global ‘PrintMessage’ (a nil value)

  1. unknown - lua/autorun/lerolljenkins.lua:2
  2. unknown - lua/includes/modules/concommand.lua:54

Any ideas ? By the way, my file is calles theroll.lua ( in autorun )

                        Thank you !

concommand.Add( "rollthedice", function( ply )
	PrintMessage( HUD_PRINTTALK, ply:Nick().." has rolled a "..math.random(1,100)..".")
end )

function RollCommand( pl, text, teamonly )
 if text == "/roll" then 
  pl:ConCommand( "rollthedice")
  return ""
hook.Add( "PlayerSay", "Chat", RollCommand )

it works for me?

edit: Also i noticed you had your stuff in lua/autorun, you need to put it in /lua/autorun/server

put it in autorun/server/

Ah thank you it finally works ! I only had to put it in the Autorun -> server , thank you everyone, it’s very nice ! I really do appreciate it ! :wink:

Strange. It worked fine for me just in autorun.

Well, may be it depends of the host ?

Or he just used

if (SERVER) then

Wow all these posts just for a simple thing to do.

Ok so basicaly what I use is a home programmed AI to detect words that shouldn’t be used, then I block those chat lines. After you can just check for the right command and exec action depending on command string.
However, I can’t help you about cancelling player chat systems, because had never worked on my side, so to make it actualy working I destroyed Source Engine default chat box, and remade the complete chatting system including packet system !

if (SERVER) then
	ChatManager = {}
	function ChatManager:RegisterChatCommand(id, name, func, isAdmin)
		local mat = {}
		mat[0] = name
		mat[1] = func
		mat[2] = isAdmin
		table.insert(commandsMap, id, mat)
	//now just call ChatManager:RegisterChatCommand([INT] CMD_Index, [String] CMD_String, function(ply, args) end, [Boolean] CMD_NeedAdmin)

	function ChatManager:GetCommandFunction(cmd, name, admin)
		for k, v in pairs(commandsMap) do
			local matrice = v
			if ((cmd .. matrice[0]) == name) then
				if (matrice[2]) then
					if (admin) then
						return matrice[1]
						return nil
					return matrice[1]
		return nil

	hook.Add("PlayerSay", "CHAT_HANDLER", fucntion(ply, text, public)
		//Don't know what is public so I'm not using it
		//text = ChatManager.ControlBot:ApplyCharCodes(text) Install your own AI parser here ; I can't give you my AI, due to the fact it's incompatible with DarkRP, and it's protected by SLDT.
		if (string.StartWith(text, "<<")) then		
			local b = true //ChatManager.ControlBot:ParseCommand(ply, text) Install your own AI parser here ; I can't give you my AI, due to the fact it's incompatible with DarkRP, and it's protected by SLDT.
			if (!b) then
				//GameGlobal:SendColoredMessageToPlayer(ply, "255, 0, 0, 255", "[ChatControlBot] -> Cette commande vous a été refusée !")
				//Don't make attention to GameGlobal : it's part of RealisticRoleplay Roleplay's Engine
				return false
			local t = string.Split(text, " ")
			local func = ChatManager:GetCommandFunction("<<", table.GetFirstValue(t), ply:IsAdmin())
			if (func == nil) then
				//GameGlobal:SendColoredMessageToPlayer(ply, "255, 0, 0", "Unknown command " .. table.GetFirstValue(t))
				//Same thing here
				return false
				text = string.Replace(text, table.GetFirstValue(t), "")
				local t = string.Split(text, " ")
				func(ply, t)
				return false