need help making simple roll the dice

Hey i need help with a project that i’m doing it is a very some project. I’m a beginner lua coder and was trying to make a simple RTD that jsut printed out a number you got from 1-100. Can someone help please


local function Roll ()

    local roll = math.random(1, 100)
    
				ply:ChatPrint("You rolled " .. roll .. "!")
    end
DarkRP.defineChatCommand("roll", roll)

What is your question, please ?

Only what i can see is that “ply” isnt defined in your function and Roll != roll you must use the exact function name

I’m just trying to get it say in chat ‘Techwarfare has rolled 50’ after you type !roll

You got errors (which ones) ? You want include your “rtd” into darkrp ? You want to create a stand alone addon ?

I get no errors and a stand alone addon

Put ply in the parenthesis of Roll. That should allow it to print.



local function Roll ( ply )

    local roll = math.random(1, 100)
    
				ply:ChatPrint("You rolled " .. roll .. "!")
    end
DarkRP.defineChatCommand("roll", roll)


Still not getting anything

Quickly wrote this. (Clientside)
[lua]
function RollTheDice(ply, number1, number2)
local RTDResult = math.random(number1, number2)
ply:ChatPrint(“You rolled “… RTDResult …”!”)
end

RTD = {}
RTD.command = “!rtd”
RTD.failmessage = “You can only roll numbers, mate!”

hook.Add(“PlayerSay”, “RTD”, function(ply,text)
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 or tonumber(text[3]) == nil then
ply:ChatPrint(RTD.failmessage)
return “”
end
if text[2] then
if text[3] then
RollTheDice(ply, tonumber(text[2]), tonumber(text[3]))
end
end
return “”
end
end)
[/lua]

Where do i place that code?

garrysmod/lua/autorun/client/rollthedice.lua OR garrysmod/addons/rollthedice/lua/autorun/client/rollthedice.lua
Then create the rollthedice.lua, and put the code in there.

Nothing happens

Oh right, I messed up the hook. This should work.

[lua]
function RollTheDice(ply, number1, number2)
local RTDResult = math.random(number1, number2)
ply:ChatPrint(“You rolled “… RTDResult …”!”)
end

RTD = {}
RTD.command = “!rtd”
RTD.failmessage = “You can only roll numbers, mate!”

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 or tonumber(text[3]) == nil then
ply:ChatPrint(RTD.failmessage)
return “”
end
if text[2] then
if text[3] then
RollTheDice(ply, tonumber(text[2]), tonumber(text[3]))
end
end
return “”
end
end)
[/lua]

OnPlayerChat returns a boolean, not a string. Also, RollTheDice is uselessly global.

[editline]30th August 2014[/editline]

Also, sending in number ranges defeats the purpose; make it 1 to 100 by default.

There was no difference when I tested, but I guess you’re right.

[lua]
local function RollTheDice(ply, number1, number2)
local RTDResult = math.random(number1, number2)
chat.AddText("You rolled ", RTDResults, “!”)
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]