GMod|想要制作一个简单的命令,在发件人聊天框中打印彩色消息



我只想制作一个简单的脚本,在执行特定命令后,在发送者的聊天中打印一个彩色文本。

首先控制台给了我一个错误[试图索引全局"chat"(零值)]。在重新加载Singleplayer并打开脚本后,它什么也没做。

当前代码:

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 )

我希望有人能帮助我。

您正在使用LocalPlayer()(仅称为客户端)和聊天。AddText()(同样,仅称为客户端)位于"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)

最新更新