How would i make a /roll command?

How would i make a command that when you type /roll it generates a random number for 1 to 100? I am not good at all with chat commands

Look into

The hell would you need net for? Just use the clientside chat hook, OnPlayerChat.

PlayerSay would be the correct location ( prevents players / clients from “weighting” the dice )…

And, Author is also correct in one way; You wouldn’t need to network the result if you used a built in chat function such as: http://wiki.garrysmod.com/page/Player/ChatPrint – If you want to add colors, etc, then you’d need to network the result.

Here are some ways to set up chat commands… The first has 2 examples, one which just redirects !* and /* to console commands, the second does the same but introduces a new command… chatcommand.Add because it adds a console command with salt and pepper ( pre/postfixes ) to prevent people from using the command in console unless they knew the pre/postfixes… The second file shows alternative ways to set up commands…

https://dl.dropboxusercontent.com/u/26074909/tutoring/chat_commands/chat_commands.lua.html

https://dl.dropboxusercontent.com/u/26074909/tutoring/chat_commands/chat_commands_other.lua.html

Hopefully these tutorials and information helps. As always, to view the Lua from any of my tutorials to enable copy/pasting ( HTML doesn’t copy/paste well in terms of HTML / CSS Highlighted Lua ), remove .html from the url.

i tried from your second example, would this work?
[lua] local CHAT_COMMANDS = {

    [ "/roll" ] = function( _p )
        _p:ConCommand( "roll" );
        local rolll = math.random( 0, 100 ) ;
        print( rolll );
    end;


};[/lua]

This is a good question for covering a few topics, so let’s go through it step-by-step

Firstly - what do we want to make?
- A /roll command, which allows players to roll the dice from 1 - 100. Let’s add colors, too.

Ok, now we need to figure out the scope of the project.
As Author said, we could just make it a clientside file, but that means each client will (probably) get a different number, which we don’t want.
Instead, we’ll make it a Shared file, so we have access to both the Client and the Server realms (if you’re unsure of what this means, imagine that the Client realm is for things that your computer handles, while the Server handles most of the game, and connecting the players.
Shared means that the file will be run on both realms.

So, mostly all lua files are run from "garrysmod/lua/autorun/", which is where our file will go - let’s call it “sh_roll_the_dice.lua” (you don’t need the ‘sh_’ but it’s good for organizational reasons)
All files in autorun/
are sent to the client automatically, so we don’t need to worry about that.

Now lets set up our file:

[LUA]
if CLIENT then

return

end
[/LUA]

This means that the code will be run if the CLIENT is executing it. Adding return before the end makes the code essentially stop - which means the Client will stop reading from there, and the rest will be run on the server.
Now let’s set up the networking

[LUA]
if CLIENT then
local function ReceiveRollMsg()

end
net.Receive( "rtd_roll", ReceiveRollMsg )
return

end

util.AddNetworkString( “rtd_roll” )
[/LUA]

The client now has a function to run when it receives the message “rtd_roll”. However, the server needs to tell the client that this message will be sent, so we use the function util.AddNetworkString
Now we can make the ‘chat command’

[LUA]
if CLIENT then
local function ReceiveRollMsg()

end
net.Receive( "rtd_roll", ReceiveRollMsg )
return

end

util.AddNetworkString( “rtd_roll” )

local function PlayerChatMessage( ply, text )

end
hook.Add( “PlayerSay”, “rtd_chat_command”, PlayerChatMessage )
[/LUA]

Now whenever someone adds a chat message, the function PlayerChatMessage will be called. This is thanks to the hook library, which basically allows you to do things when something happens.

[LUA]
if CLIENT then
local function ReceiveRollMsg()

end
net.Receive( "rtd_roll", ReceiveRollMsg )
return

end

util.AddNetworkString( “rtd_roll” )

local cooldown_time = 4
local function PlayerChatMessage( ply, text )
if text:lower() ~= “/roll” then return end
if ply.LastRollTime and CurTime() - ply.LastRollTime < cooldown_time then return end
ply.LastRollTime = CurTime()

local number = math.random( 1, 100 )
net.Start( "rtd_roll" )
	net.WriteEntity( ply )
	net.WriteInt( number, 8 )
net.Broadcast()

return ""

end
hook.Add( “PlayerSay”, “rtd_chat_command”, PlayerChatMessage )
[/LUA]

Our code now broadcasts the message “rtd_roll” whenever someone types /roll in chat, containing the player who sent it and a random number from 1-100. It also stops players from spamming it, by comparing the last time they used the command with the current time
Returning an empty string makes the original message not appear in chat.

Finally, let’s make the client do something when they receive the message.

[LUA]
if CLIENT then
local num_color = Color( 80, 190, 60 )
local player_color = Color( 160, 120, 60 )

local function ReceiveRollMsg()
	local ply = net.ReadEntity()
	local num = net.ReadInt( 8 )
	
	chat.AddText( player_color, ply:Nick(), color_white, " rolled the dice, and got ", num_color, tostring( num ), color_white, "!" )
end
net.Receive( "rtd_roll", ReceiveRollMsg )
return

end

util.AddNetworkString( “rtd_roll” )

local cooldown_time = 4
local function PlayerChatMessage( ply, text )
if text:lower() ~= “/roll” then return end
if ply.LastRollTime and CurTime() - ply.LastRollTime < cooldown_time then return end
ply.LastRollTime = CurTime()

local number = math.random( 1, 100 )
net.Start( "rtd_roll" )
	net.WriteEntity( ply )
	net.WriteInt( number, 8 )
net.Broadcast()

return ""

end
hook.Add( “PlayerSay”, “rtd_chat_command”, PlayerChatMessage )
[/LUA]

There you have it - a working command.
If you want to try extending yourself, why not try making the player able to pick the random number choice? So “/roll 1 30” would pick a number between 1 and 30. Or maybe send the player a message if they’re trying to spam the command?
Hope all that made sense, if you have any questions feel free to ask.
Good luck!

Instead of all that, you could also just set the text ( return “” means no text, while return “rolled a " … math.random( 1, 100 ) … " out of 100!”; would show all players the value the player rolled )…

And, that would work, but since the function is there you no longer need a console command. Here’s the shortest method of doing it:


hook.Add( "PlayerSay", "Acecool:ChatCommands", function( _p, _msg, _team )
	// We'll use startwith just in case the user made a typo...
	local _bRolled = string.StartWith( string.lower( _msg ), "/roll" );

	// If they rolled
	if ( _bRolled ) then
		// Return the roll text, along with the value...
		return "Rolled a " .. math.random( 1, 100 ) .. " out of 100!";
	end
end );

You could also use string.sub instead of string.StartWith which means we can skip the first char so /, !, ^, & etc followed by roll would do the same thing…


hook.Add( "PlayerSay", "Acecool:ChatCommands", function( _p, _msg, _team )
	// We'll use string.sub just in case the user made a typo with the /, !, etc or /rolls etc...
	local _bRolled = string.sub( string.lower( _msg ), 2, 5 ) == "roll";

	// If they rolled
	if ( _bRolled ) then
		// Return the roll text, along with the value...
		return "Rolled a " .. math.random( 1, 100 ) .. " out of 100!";
	end
end );

Many different ways to do it. If it is only supposed to output text, it is easier just to do it inside the hook that go off using other methods.

Using first example:


//
// Type !command to run a console command from the player, publically for all to see.
// Type /command to run a console command from the player, privately for none to see.
//
hook.Add( "PlayerSay", "Acecool:ChatCommands", function( _p, _msg, _team )
	// Do /commands privately, !commands publicly
	local _bSlash = string.StartWith( _msg, "/" );
	local _bExclaim = string.StartWith( _msg, "!" );

	if ( _bSlash || _bExclaim ) then
		// This executes the command
		_p:ConCommand( string.sub( _msg, 2 ) );

		// This hides / commands from being displayed
		if ( !_bExclaim ) then
			return "";
		end
	end
end );


//
// Executed when !roll or /roll is typed in chat.
//
concommand.Add( "roll", function( _p, _cmd, _args )
	local _roll = math.random( 1, 100 );
	for k, v in pairs( player.GetAll( ) ) do
		// Skip NULL / unset player objects
		if ( !IsValid( v ) ) then continue; end

		// Output to all players...
		v:ChatPrint( tostring( _p ) .. " Rolled a " .. _roll .. " out of 100!";
	end
end );

Other examples would be the same except on how the function is added. Second would be chatcommand.Add instead of concommand.Add. Third would simply replace the contents of the function with the contents of the function above.

[lua]
local function RollTheDice(ply, number1, number2)
local RTDResult = math.random(number1, number2)
chat.AddText("You rolled ", RTDResult, “!”)
end

RTD = {}
RTD.command = “!rtd”

hook.Add(“OnPlayerChat”, “RTD”, function(ply,text)
if not ply == LocalPlayer() then return end
local text = string.Explode(" ", text)
if string.sub(string.lower(text[1]), 1, string.len(RTD.command)) == string.lower(RTD.command) then
if tonumber(text[2]) == nil then text[2] = 1 end
if tonumber(text[3]) == nil then text[3] = 100 end
if text[2] then
if text[3] then
RollTheDice(ply, tonumber(text[2]), tonumber(text[3]))
end
end
return false
end
end)
[/lua]

why do u need the ply into the function if u dont use it anyways? :stuck_out_tongue:

And the client is rolling the dice; never trust client input, especially if the roll is meant for the server to do something with. My example was shorter, and more “secure” with less code ( but didn’t support the start / stop for the roll ).

Additionally, you’re doing checks on text sub 2 and 3 checking for nil ( should just do a ! check instead ) but the if statements become redundant because you already process them for values.



hook.Add( "PlayerSay", "Rolls a dice number 1-100" function( ply, text ) 
	local rolldice = math.Clamp( 1, 100 ) 
	if ( ply:Text() == "!roll" ) then
		ply:ChatPrint( ply:Nick().." has rolled "..rolldice.."!" )

	end


WTH is ply:Text() :P, pretty sure you ment ‘text’…

Thank you, this worked very well! I understand now. Thank you very much

[editline]22nd November 2014[/editline]

i have one question, where would i put this? i tried making an addon with it and puting it in lua/autorun/client

[editline]22nd November 2014[/editline]

nobody knows?

Quite funny how you’re trying to steal ideas off the server you were banned from.

Sincerely,
Anonymous

Grow up.

Sincerely,
Phoenixf129

ur funny

sincerely,
Tomis13lack2

so, where would i put this? addons/roll/lua/autorun OR addons/roll/lua/autorun/client or what?