Adding a chat command function - ConCommand wont work
29 replies, posted
Hello.
I'm new at the fantastic world of LUA, and I'm trying to make some simple scripts to get me started.
I've looked a little bit into DarkRP, and researched that it has an AddChatCommand function.
Found it here: [url]http://maurits.tv/data/garrysmod/wiki/wiki.garrysmod.com/index88ce.html[/url]
Then i thought i would try to make a function where the player writes: !stuck in his chat. It will make him send an message to online admins (Using ULX's "@" command)
My script is located in: garrysmod\lua\autorun\server and it's called stuck.lua
Here is what i've made so far:
[code]
local function CallAdmin(ply, args)
ply:ChatPrint("Test") // I've made this simple line for debugging. When I'm typing !stuck this message is displayed.
ply:ConCommand("say", "@ I'm stuck and need an admin.") // This wont work.
return "" // My research told me that this is needed in DarkRP, else everyone would be able to see the "!stuck" part.
end
hook.Add( "CallAdmin", CallAdmin ) // Do I need this, and why?
AddChatCommand("!stuck", CallAdmin)
[/code]
Okay, I know this probably could be coded much better. I've tried reading the guides here: [url]http://maurits.tv/data/garrysmod/wiki/wiki.garrysmod.com/index7a06.html[/url]
But I've seem to have some troubles linking it all together. I think I get the function itself and the ConCommand also seems fine to me (found documentation about it here: [url]http://maurits.tv/data/garrysmod/wiki/wiki.garrysmod.com/index8d04.html[/url] )
What I'm not so sure about is the hook.add - What is the point of this?
Also I might need some help on the arguments of the function. How do I know which arguments the function takes? If you have bonus info, I would be happy to recive it.
I have no errors in my console(client/server)
I thank you for your time, and i would appreciate if could help me understand LUA by giving me a detailed answer. That way I hope to learn something from this.
EDIT: Oh, a little OT. If anyone have a decent understanding for LUA and have a little time, i would be really grateful to have a mentor. Nothing special needs. It would just be nice to network a little and get some advices/tips for someone far better to LUA than me. I would prefer if you speak English or Danish. Thanks in advance!
try ply:ConCommand("cmd", "say @ I'm stuck and need an admin")
you don't need the hook.Add
Hello Banana Lord, and thank you for trying to help me, I appreciate that.
But i have to say that it still doesn't work.
This is the code I tried to use:
[code]
local function testChat(ply, args)
ply:ChatPrint("Hej")
ply:ConCommand("cmd", "say @ I'm stuck and need an admin")
return ""
end
AddChatCommand("!stuck", testChat)
[/code]
Any other ideas I might try?
oops, try ply:ConCommand("cmd say @ I'm stuck and need an admin")
Returns:
FCVAR_SERVER_CAN_EXECUTE prevented server running command: cmd
In my console (player's console)
Seems like the command is blocked. :/
[editline]13th August 2013[/editline]
Also tried this, without luck. I even think I tried to make more lines, also using LocalPlayer: and so on.(LocalPlayer():ConCommand is clientside i guess)
[code]
ply:ConCommand("say Hello World!")
ply:ConCommand("say", "@ I'm stuck and need an admin")
ply:RunConsoleCommand("say", "Hello, I didn't type this.")
RunConsoleCommand("say Hello, I didn't type this.")
ply:RunConsoleCommand("say", "Hello, I didn't type this.")
ply:RunConsoleCommand("say Hello, I didn't type this.")
[/code]
how about ply:SendLua([[RunConsoleCommand("cmd", "say", "@ I'm stuck and need an admin")]])
How about this:
[CODE]
--Stuck v2
--Pootis on serverside only, in that stuck.lua you were makin'
function AdminHelpReqStuck(ply)
for k,v in pairs(player.GetAll()) do
if v:IsAdmin() then --Only Admins/SAdmins allowed to see
-- Why use SendLua or Commands, PrintMessage is fun and you can even make it appear in the middle of the screen by
-- Changing "HUD_PRINTTALK" to "HUD_PRINTCENTER"
-- Check "http://wiki.garrysmod.com/page/Enums/HUD"
v:PrintMessage(HUD_PRINTTALK, "Player, ".. ply:Nick() .. ", requires an super hero to free him from the evil wall of doom")
end
return ""
end
end
AddChatCommand("!stuck", AdminHelpReqStuck)
[/CODE]
You'll need a [b]return ""[/b] at the end of the loop
[QUOTE=Banana Lord.;41829980]You'll need a [b]return ""[/b] at the end of the loop[/QUOTE]
I don't think so, because its self implied on the code
[QUOTE=andre_bule;41830005]I don't think so, because its self implied on the code[/QUOTE]
If you don't add it "!stuck" will still relay to chat
[QUOTE=andre_bule;41830005]I don't think so, because its self implied on the code[/QUOTE]
You do because of how darkrp's AddChatCommand works. Unless it changed recently.
[QUOTE=BL00DB4TH;41830019]You do because of how darkrp's AddChatCommand works. Unless it changed recently.[/QUOTE]
Oh, shit I remembered lol xD
Banana Lord -> Thanks for the help. It seems like all of those commands are blocked, which is really annoying.
andre_bule -> Thank you also! I tried you script and it is working. It would be great if i could use the ULC method of calling an admin (using @) - But that doesn't seem to work, so thank you for this solution.
Is there any way to color the
[code]
v:PrintMessage(HUD_PRINTTALK, "Player, ".. ply:Nick() .. ", requires an super hero to free him from the evil wall of doom")
[/code] message?
[QUOTE=Sam7100;41833914]Banana Lord -> Thanks for the help. It seems like all of those commands are blocked, which is really annoying.
andre_bule -> Thank you also! I tried you script and it is working. It would be great if i could use the ULC method of calling an admin (using @) - But that doesn't seem to work, so thank you for this solution.
Is there any way to color the
[code]
v:PrintMessage(HUD_PRINTTALK, "Player, ".. ply:Nick() .. ", requires an super hero to free him from the evil wall of doom")
[/code] message?[/QUOTE]
PrintMessage doesn't have that functionality although, chat.AddText has.
Making a script for you atm
[editline]14th August 2013[/editline]
Here it is, after 10 thousand years in development, hopefully it will be worth the weight/wait, thanks and have fun :D
Although I must say there is another way of doing this (usermessages (not really messages that you see like in chat or something))
Check [URL="http://wiki.garrysmod.com/page/usermessage"]http://wiki.garrysmod.com/page/usermessage[/URL]
[CODE]
--Stuck v3
--Pootis on serverside only, or in that stuck.lua you were makin'
--[Added] colors :D
-- Options red, green, blue, random, now choose
local WhichColor = "random"
function AdminHelpReqStuck(ply)
for k,v in pairs(player.GetAll()) do
if v:IsAdmin() then --Only Admins/SAdmins allowed to see
--Example: " Color( Red, Green, Blue ), "MESSAGE" "
--Note: You can add multiple colors
--Example: " "Hi", Color( R, G, B ), "Text with Colar" "
--Use \ if something like this occurs "LOL"Lol"", it must be like "LOL\"lol\""
if WhichColor == "red" or WhichColor == "Red" then
v:SendLua("chat.AddText(Color(255, 0, 0), \"" .. ply:Nick() .. " requires a supahero to save him from the evil wall\")")
v:SendLua("chat.PlaySound()") --Sending sounds (that click)
elseif WhichColor == "blue" or WhichColor == "Blue" then
v:SendLua("chat.AddText(Color(0, 0, 255), \"" .. ply:Nick() .. " requires a supahero to save him from the evil wall\")")
v:SendLua("chat.PlaySound()") --Sending sounds (that click)
elseif WhichColor == "green" or WhichColor == "Green" then
v:SendLua("chat.AddText(Color(0, 255, 0), \"" .. ply:Nick() .. " requires a supahero to save him from the evil wall\")")
v:SendLua("chat.PlaySound()") --Sending sounds (that click)
elseif WhichColor == "random" or WhichColor == "Random" then
v:SendLua("chat.AddText(Color(math.random(1, 255), math.random(1, 255), math.random(1, 255)), \"" .. ply:Nick() .. " requires a supahero to save him from the evil wall\")")
v:SendLua("chat.PlaySound()") --Sending sounds (that click)
else
Msg("Somethings Wrong... \n") --Check WhichColor if it occurs
end
end
return "" --changed
end
end
AddChatCommand("!stuck", AdminHelpReqStuck)
[/CODE]
Edit:
The code is wrong
[QUOTE=andre_bule;41834125]PrintMessage doesn't have that functionality although, chat.AddText has.
Making a script for you atm
[editline]14th August 2013[/editline]
Here it is, after 10 thousand years in development, hopefully it will be worth the weight/wait, thanks and have fun :D
Although I must say there is another way of doing this (usermessages (not really messages that you see like in chat or something))
Check [URL="http://wiki.garrysmod.com/page/usermessage"]http://wiki.garrysmod.com/page/usermessage[/URL]
[CODE]
--Stuck v3
--Pootis on serverside only, or in that stuck.lua you were makin'
--[Added] colors :D
-- Options red, green, blue, random, now choose
local WhichColor = "random"
function AdminHelpReqStuck(ply)
for k,v in pairs(player.GetAll()) do
if v:IsAdmin() then --Only Admins/SAdmins allowed to see
--Example: " Color( Red, Green, Blue ), "MESSAGE" "
--Note: You can add multiple colors
--Example: " "Hi", Color( R, G, B ), "Text with Colar" "
--Use \ if something like this occurs "LOL"Lol"", it must be like "LOL\"lol\""
if WhichColor == "red" or WhichColor == "Red" then
v:SendLua("chat.AddText(Color(255, 0, 0), \"" .. ply:Nick() .. " requires a supahero to save him from the evil wall\")")
v:SendLua("chat.PlaySound()") --Sending sounds (that click)
elseif WhichColor == "blue" or WhichColor == "Blue" then
v:SendLua("chat.AddText(Color(0, 0, 255), \"" .. ply:Nick() .. " requires a supahero to save him from the evil wall\")")
v:SendLua("chat.PlaySound()") --Sending sounds (that click)
elseif WhichColor == "green" or WhichColor == "Green" then
v:SendLua("chat.AddText(Color(0, 255, 0), \"" .. ply:Nick() .. " requires a supahero to save him from the evil wall\")")
v:SendLua("chat.PlaySound()") --Sending sounds (that click)
elseif WhichColor == "random" or WhichColor == "Random" then
v:SendLua("chat.AddText(Color(math.random(1, 255), math.random(1, 255), math.random(1, 255)), \"" .. ply:Nick() .. " requires a supahero to save him from the evil wall\")")
v:SendLua("chat.PlaySound()") --Sending sounds (that click)
else
Msg("Somethings Wrong... \n") --Check WhichColor if it occurs
end
end
return ""
end
end
AddChatCommand("!stuck", AdminHelpReqStuck)
[/CODE][/QUOTE]
Sorry to inform you that your script only allows one person to see it. Like if we are to admins on the server, and there is one user, only one of the admins will be able to see it.
[QUOTE=Sam7100;41835523]Sorry to inform you that your script only allows one person to see it. Like if we are to admins on the server, and there is one user, only one of the admins will be able to see it.[/QUOTE]
Ermm no due to the For loop, it runs through all players and checks each one of them and then if they are admin it sends the lua containing the message.
P.S.: [U]Nothing [/U] is sent to players which aren't admins!
EDIT:
I was wrong
[QUOTE=andre_bule;41835545]Ermm no due to the For loop, it runs through all players and checks each one of them and then if they are admin it sends the lua containing the message.
P.S.: [U]Nothing [/U] is sent to players which aren't admins![/QUOTE]
First of all, "SendLua" is horrible in every way, second of all you're ending the function at the return "" line which would make Sam7100 correct as it'd only ever send to 1 player (and in most cases, unless the first player it loops through is an admin it won't send it to anybody at all).
[QUOTE=EvacX;41835993]First of all, "SendLua" is horrible in every way, second of all you're ending the function at the return "" line which would make Sam7100 correct as it'd only ever send to 1 player (and in most cases, unless the first player it loops through is an admin it won't send it to anybody at all).[/QUOTE]
The op is [U]learning[/U] Lua, teaching harder methods in such skill is not wise, therefore I chose to make him a SendLua script and not a usermessage one.
In DarkRP, AddChatCommand requires a return "" and it seems that you don't know what is a for loop.
[QUOTE=andre_bule;41836162]The op is [U]learning[/U] Lua, teaching harder methods in such skill is not wise, therefore I chose to make him a SendLua script and not a usermessage one.
In DarkRP, AddChatCommand requires a return "" and it seems that you don't know what is a for loop.[/QUOTE]
You are doing return "" [I]inside[/I] the bloody for loop and it has no conditional parameters so it will [I]always[/I] end the function before the first iteration of the loop is finished. And really? A usermessage script? You're supposed to use [b][url=http://gmodwiki.net/Lua/Libraries/net]Lua/Libraries/net[img]http://gmodwiki.net/favicon.ico[/img][/url][/b] which is not outdated.
Regardless, Net is not complicated, if you feel that way then I think it's time to step down as a self-proclaimed coder. If you're gonna try and teach a person Lua you might as well do it correctly instead of having him inherit those terrible pratices such as those which you are teaching him.
If you're gonna try and lecture me on Lua I'd suggest you actually educate yourself on the subject first.
-Snip, I saw where I was wrong, sorry to be stubburn :/-
Hey guys. Thank you for spending some time on helping me. I really hope that you are aware of how much this already has helped me. I got some few strings from the debate that i have been doing some research for and think i've learned a little.
Though I'm still a little confused. Because I want to learn lua - also the right way ;-) - But in which direction should I go? I still belive that my first post SHOULD be possible, because i got it working for a week ago, but then made an edit in it and now I can't remember how the script actually was, which sucks!
What do you guys think?
Hey OP, I made a chat command framework recently. It uses a different approach. I am not trying to use the "say" command, but instead I catch the text a player wrote by using the PlayerSay hook.
Here is the code. This is a framework, so adding a new command is very simple using the AddCommand. Running it is automatic.
How it works: the script catches the string of a message, and if it finds a "/" that means it is a command. Then it checks if there are any other words in the message, and if there are, it uses them as arguments.
This way you can add commands that require users to enter arguments too, expanding possiblities.
For example it is easy to make a command "/give" that requires 1 argument - weapon class - and gives the player a weapon of that class.
Ok, hope I explained it good enough. Here is the code. It's from a gamemode, but I hope you get the idea.
[code]
MODULE.comms = { }
// Server side
// Commands
function MODULE:AddCommand( args )
local commTable =
{
name = args.name,
desc = args.desc,
func = args.func,
arguments = args.arguments,
access = args.access or "any"
}
MODULE.comms[args.comm] = commTable
end
function MODULE:RunCommand( ply, command, args )
if self.comms[command] then
if self.comms[command]["access"] != "any" then
if self.comms[command]["access"] == "admin" and ply:GetNWString("usergroup") != "admin" then
if ply:GetNWString("usergroup") != "superadmin" then
print("You don't have permission to use this command!") return end
end
if self.comms[command]["access"] == "superadmin" and ply:GetNWString("usergroup") != "superadmin" then
print("You don't have permission to use this command!") return end
end
if table.Count(args) == self.comms[command]["arguments"] then
self.comms[command].func(ply, args)
else print("That command requires ".. self.comms[command]["arguments"] .. " arguments, got " ..table.Count(args)) return end
else print("Invalid command") return end
end
// Client side
// Client side hooks
function MODULE:PlayerSay( ply, strText, teamchat )
if string.sub( tostring(strText), 0, 1 ) == "/" then
strText = string.lower(string.TrimRight(strText))
local comm
local args = { }
if string.find(strText, " ") then
comm = string.Trim(string.sub(strText, 2, string.find(strText, " ") ))
args = string.Explode(" ", string.sub(strText, string.find(strText, " ") + 1, string.len(strText) ))
else
comm = string.sub(strText, 2)
end
self:RunCommand(ply, comm, args )
end
end
//Test command for debug purporses
/*
function Test(ply, args)
ply:ChatPrint("Module works")
end
MODULE:AddCommand(
{
comm = "test",
name = "Test command",
desc = "Check if it works",
func = Test,
arguments = 0
}
)
*/
[/code]
EDIT:
Something is wrong with me, so I didn't even get what the thread was about. Sorry for misleading.
I will keep that post, just in case someone will find it useful. Now I really need to go to sleep.
Well, i got it working now - sort of.
Here is my code:
[code]
--local WhichColor = "green"
function AdminHelpReqStuck(ply)
for k,v in pairs(player.GetAll()) do
if v:IsUserGroup("mod") or v:IsUserGroup("admin") or v:IsUserGroup("superadmin") or v:IsUserGroup("owner") then
v:SendLua("chat.AddText(Color(0, 255, 0), \"" .. ply:Nick() .. " is stuck and need an admin!\")")
v:SendLua("chat.PlaySound()") --Sending sounds (that click)
else
ply:ChatPrint("A Staff member has been notified... \n")
v:SendLua("chat.PlaySound()")
timer.Create( "my_timer", 20, 1, function()
ply:ChatPrint("No admins responding? Ask a player to kill you, or reconnect. \n")
v:SendLua("chat.PlaySound()")
end)
end
-- return "" // Disabled - If this is on the script wont work.
end
end
AddChatCommand("!stuck", AdminHelpReqStuck, "AdminHelpReqStuck")
[/code]
When i start the server it says:
[ERROR] lua/autorun/server/stuck.lua:20: attempt to call global "AddChatCommand" <a nil value>
1. unknown - lua/autorun/server/stuck.lua:20
And the !stuck command doesn't work. No errors, nothing. The player are able to see the command he just wrote.
If I change this line:
[code]
-- return "" // Disabled - If this is on the script wont work.
[/code]
by removing the to "--" saves it, then adds the two "--" again, saves and eerything works fine.
Just tried to reinstall DarkRP, still same problem.
Any suggestion?
EDIT: Seems like I just have to edit SOMETHING in the stuck.lua, then save it and it works again.
[QUOTE=Sam7100;41860244]Well, i got it working now - sort of.
Here is my code:
[code]
--local WhichColor = "green"
function AdminHelpReqStuck(ply)
for k,v in pairs(player.GetAll()) do
if v:IsUserGroup("mod") or v:IsUserGroup("admin") or v:IsUserGroup("superadmin") or v:IsUserGroup("owner") then
v:SendLua("chat.AddText(Color(0, 255, 0), \"" .. ply:Nick() .. " is stuck and need an admin!\")")
v:SendLua("chat.PlaySound()") --Sending sounds (that click)
else
ply:ChatPrint("A Staff member has been notified... \n")
v:SendLua("chat.PlaySound()")
timer.Create( "my_timer", 20, 1, function()
ply:ChatPrint("No admins responding? Ask a player to kill you, or reconnect. \n")
v:SendLua("chat.PlaySound()")
end)
end
-- return "" // Disabled - If this is on the script wont work.
end
end
AddChatCommand("!stuck", AdminHelpReqStuck, "AdminHelpReqStuck")
[/code]
When i start the server it says:
[ERROR] lua/autorun/server/stuck.lua:20: attempt to call global "AddChatCommand" <a nil value>
1. unknown - lua/autorun/server/stuck.lua:20
And the !stuck command doesn't work. No errors, nothing. The player are able to see the command he just wrote.
If I change this line:
[code]
-- return "" // Disabled - If this is on the script wont work.
[/code]
by removing the to "--" saves it, then adds the two "--" again, saves and eerything works fine.
Just tried to reinstall DarkRP, still same problem.
Any suggestion?
EDIT: Seems like I just have to edit SOMETHING in the stuck.lua, then save it and it works again.[/QUOTE]
If you move the return "" to near the last "end" it will work, the return "" is basically to not "see" the command in the chat, but it would be a good idea so the command gets spread.
Btw in ChatPrint you don't need "\n".
The lua folder runs before the gamemode. Run this in an [url=http://wiki.garrysmod.com/page/GM/Initialize]Initialize[/url] hook
[QUOTE=Banana Lord.;41862162]The lua folder runs before the gamemode. Run this in an [url=http://wiki.garrysmod.com/page/GM/Initialize]Initialize[/url] hook[/QUOTE]
Or he could do "include("stuck.lua")" on init or put it there
[QUOTE=andre_bule;41862217]Or he could do "include("stuck.lua")" on init or put it there[/QUOTE]
He doesn't have to edit core files if he uses a hook
[editline]16th August 2013[/editline]
Reduces risk of losing the code in a DarkRP update
[QUOTE=Banana Lord.;41862301]He doesn't have to edit core files if he uses a hook
[editline]16th August 2013[/editline]
Reduces risk of losing the code in a DarkRP update[/QUOTE]
Thank you. I made a hook (which i knew VERY little about, but read about it) and now it works really great.
I would like to thank all of you for helping me with this issue, i have learned a lot by this little script. Again, thank you and have a great weekend!
Sorry, you need to Log In to post a reply to this thread.