hey guys can anyone tell me how to make chat commands?
thanks
enzo
You need to hook into PlayerSay and check if the first word they said is a chatcommand.
[lua]
-- UberMensch's Chat Command module thing
-- Free for use, but don't remove this text ;)
-- Set your chat prefix here.
local prefix = "/"
-- Don't touch these or the entire script breaks.
chatcommand = {}
chatcommand.Commands = {}
function chatcommand.Add( command, func )
chatcommand.Commands[prefix .. command] = func
end
function chatcommand.Remove( command )
chatcommand.Commands[prefix .. command] = nil
end
function chatcommand.PlayerSay( ply, text, teamchat )
-- Explode the text into a table.
local cmd = string.Explode( " ", text )
for K, V in pairs( chatcommand.Commands ) do
-- If the first entry in the table matches an existing command...
if( string.lower(cmd[1]) == K ) then
-- Strip the command out of the text.
local cmd = string.gsub( text, cmd[1] .. " ", "" )
-- Explode the remaining text into arguments.
local args = string.Explode( " ", cmd )
-- Run the function assigned to that command.
chatcommand.Commands[K]( ply, K, args )
-- Return false to prevent text being displayed.
return false
end
end
-- No chat command matched? Return all the text as normal.
return text
end
hook.Add( "PlayerSay", "chatcommand_PlayerSay", chatcommand.PlayerSay )
[/lua]
You can use that the same way as concommand.Add():
[lua]
function chatTesting( ply, cmd, args )
ply:ChatPrint( "You said " .. string.Implode( " ", args ) )
end
chatcommand.Add( "testing", chatTesting )
[/lua]
[QUOTE=UberMensch;17041341]You need to hook into PlayerSay and check if the first word they said is a chatcommand.
[lua]
-- UberMensch's Chat Command module thing
-- Free for use, but don't remove this text ;)
-- Set your chat prefix here.
local prefix = "/"
-- Don't touch these or the entire script breaks.
chatcommand = {}
chatcommand.Commands = {}
function chatcommand.Add( command, func )
chatcommand.Commands[prefix .. command] = func
end
function chatcommand.Remove( command )
chatcommand.Commands[prefix .. command] = nil
end
function chatcommand.PlayerSay( ply, text, teamchat )
-- Explode the text into a table.
local cmd = string.Explode( " ", text )
for K, V in pairs( chatcommand.Commands ) do
-- If the first entry in the table matches an existing command...
if( string.lower(cmd[1]) == K ) then
-- Strip the command out of the text.
local cmd = string.gsub( text, cmd[1] .. " ", "" )
-- Explode the remaining text into arguments.
local args = string.Explode( " ", cmd )
-- Run the function assigned to that command.
chatcommand.Commands[K]( ply, K, args )
-- Return false to prevent text being displayed.
return false
end
end
-- No chat command matched? Return all the text as normal.
return text
end
hook.Add( "PlayerSay", "chatcommand_PlayerSay", chatcommand.PlayerSay )
[/lua]
You can use that the same way as concommand.Add():
[lua]
function chatTesting( ply, cmd, args )
ply:ChatPrint( "You said " .. string.Implode( " ", args ) )
end
chatcommand.Add( "testing", chatTesting )
[/lua][/QUOTE]
hey i quite lost were do u put the file?
If you are uncertain where to put that file, it's best to learn basic Lua and file locations first.
init.lua of your gamemode
Sorry, you need to Log In to post a reply to this thread.