G模组 |想要制作一个简单的命令,在发件人聊天框中打印彩色消息
GMod | Want to make a simple command that prints a colored message in the senders chatbox
我只想做一个简单的脚本,在执行特定命令后在发件人的聊天中打印彩色文本。
首先,控制台给了我一个错误 [attempt to index global 'chat' (a nil value)]。重新加载单人游戏并打开脚本后,它什么也没做。
当前代码:
local ply = LocalPlayer()
local function Test( ply, text, team )
if string.sub( text, 1, 8 ) == "!command" then
chat.AddText( Color( 100, 100, 255 ), "Test" )
end
end
hook.Add( "PlayerSay", "Test", Test )
希望有人能帮助我。
您在 "PlayerSay" 挂钩 (这是一个服务器端挂钩)。你还需要其他东西,比如 ChatPrint()
编辑:刚刚意识到 ChatPrint() 不接受其中的 Color() 参数...您总是可以尝试发送网络消息:
if SERVER then
util.AddNetworkString( "SendColouredChat" )
function SendColouredChat( ply, text )
if string.sub( text, 1, 8 ) == "!command" then
net.Start( "SendColouredChat" )
net.WriteTable( Color( 255, 0, 0, 255 ) )
net.WriteString( "Test" )
net.Send( ply )
end
end
hook.Add( "PlayerSay", "SendColouredChat", SendColouredChat )
end
if CLIENT then
function ReceiveColouredChat()
local color = net.ReadTable()
local str = net.ReadString()
chat.AddText( color, str )
end
net.Receive( "SendColouredChat", ReceiveColouredChat )
end
编辑:几年后回到这个问题。对于以后可能 运行 参与其中的任何人来说,使用 GM:OnPlayerChat 钩子 更 更简单。
local function Command(ply, text, teamOnly, dead)
if text:sub(1, 8) == "!command" then
chat.AddText(Color(100, 100, 255), "Test")
end
end
hook.Add("OnPlayerChat", "TestCommand", Command)
我只想做一个简单的脚本,在执行特定命令后在发件人的聊天中打印彩色文本。
首先,控制台给了我一个错误 [attempt to index global 'chat' (a nil value)]。重新加载单人游戏并打开脚本后,它什么也没做。
当前代码:
local ply = LocalPlayer()
local function Test( ply, text, team )
if string.sub( text, 1, 8 ) == "!command" then
chat.AddText( Color( 100, 100, 255 ), "Test" )
end
end
hook.Add( "PlayerSay", "Test", Test )
希望有人能帮助我。
您在 "PlayerSay" 挂钩 (这是一个服务器端挂钩)。你还需要其他东西,比如 ChatPrint()
编辑:刚刚意识到 ChatPrint() 不接受其中的 Color() 参数...您总是可以尝试发送网络消息:
if SERVER then
util.AddNetworkString( "SendColouredChat" )
function SendColouredChat( ply, text )
if string.sub( text, 1, 8 ) == "!command" then
net.Start( "SendColouredChat" )
net.WriteTable( Color( 255, 0, 0, 255 ) )
net.WriteString( "Test" )
net.Send( ply )
end
end
hook.Add( "PlayerSay", "SendColouredChat", SendColouredChat )
end
if CLIENT then
function ReceiveColouredChat()
local color = net.ReadTable()
local str = net.ReadString()
chat.AddText( color, str )
end
net.Receive( "SendColouredChat", ReceiveColouredChat )
end
编辑:几年后回到这个问题。对于以后可能 运行 参与其中的任何人来说,使用 GM:OnPlayerChat 钩子 更 更简单。
local function Command(ply, text, teamOnly, dead)
if text:sub(1, 8) == "!command" then
chat.AddText(Color(100, 100, 255), "Test")
end
end
hook.Add("OnPlayerChat", "TestCommand", Command)