Hey I was wondering how they did "/advert" in DarkRP...right now I have this:
[lua]
hook.Add( "PlayerSay", "RPcommands", function( ply, text, team )
if ( string.sub( text, 1, 7 ) == "/advert" ) then
return "[Advertisement] " .. string.sub( text, 8 )
end
[/lua]
and I was wondering how I could make this text gold...do I use the Net Library or do I use ply:sendlua?
[url=https://facepunch.com/showthread.php?t=1570900]As stated in the other thread,[/url] use net messages - SendLua is wasteful and limited to 255 chars.
[QUOTE=Luni;52459962][url=https://facepunch.com/showthread.php?t=1570900]As stated in the other thread,[/url] use net messages - SendLua is wasteful and limited to 255 chars.[/QUOTE]
I have already tried the networking approach...any examples of it in use....?
Linked on that thread: [url]http://wiki.garrysmod.com/page/Net_Library_Usage[/url]
Also, [URL="https://facepunch.com/showthread.php?t=1421205&p=45838402&viewfull=1#post45838402"]you should Google before making a thread[/URL]
[QUOTE=JasonMan34;52460591]Also, [URL="https://facepunch.com/showthread.php?t=1421205&p=45838402&viewfull=1#post45838402"]you should Google before making a thread[/URL][/QUOTE]
I did exactly that yet it did not work with string.sub...was wondering if there was another way besides that...
[QUOTE=-Red-;52460665]I did exactly that yet it did not work with string.sub...was wondering if there was another way besides that...[/QUOTE]
There isn't, and you did something wrong.
What was your code?
[QUOTE=JasonMan34;52460670]There isn't, and you did something wrong.
What was your code?[/QUOTE]
[lua]
-- Server
local ply = FindMetaTable("Player")
util.AddNetworkString( "ChatColor" )
function BroadcastMsg(...)
local args = {...}
net.Start("ChatColor")
net.WriteTable(args)
net.Broadcast()
end
function ply:PlayerMsg(...)
local args = {...}
net.Start("ChatColor")
net.WriteTable(args)
net.Send(self)
end
if ( string.sub( text, 1, 7 ) == "/advert" ) then
return ply:PlayerMsg(Color(0,255,0), "[Advertisement]" .. string.sub( text, 8 ) )
end
--Client
net.Receive("ChatColor",function(len)
local msg = net.ReadTable()
chat.AddText(unpack(msg))
chat.PlaySound()
end)
--Error
[ERROR] gamemodes/rp/gamemode/init.lua:134: bad argument #1 to 'sub' (string expected, got nil)
1. sub - [C]:-1
2. unknown - gamemodes/rp/gamemode/init.lua:134
[ERROR] gamemodes/rp/gamemode/init.lua:134: bad argument #1 to 'sub' (string expected, got nil)
1. sub - [C]:-1
2. unknown - gamemodes/rp/gamemode/init.lua:134
[/lua]
Where is the variable "text" defined in that code? I don't see the chat hook anywhere.
Is [CODE]if ( string.sub( text, 1, 7 ) == "/advert" ) then
return ply:PlayerMsg(Color(0,255,0), "[Advertisement]" .. string.sub( text, 8 ) )
end[/CODE]
wrapped in a playersay hook?
else text is not being defined...
Where's your hook?
Ninja'd :v:
It was there just did not include it...forgot an ending:P
[QUOTE=-Red-;52460730]It was there just did not include it...forgot an ending:P[/QUOTE]
Well the hook will never supply a nil value as an argument, so you're doing something wrong. Can you send the code in full?
Here's a more efficient function to send colored text to the client:
[code]util.AddNetworkString("chat")
function ColorMessage(...) -- ColorMessage(Color(255, 0, 0), "This is a test")
arg = {...}
net.Start("chat")
net.WriteUInt(#arg, 16) --Send the number of arguments
for _, v in pairs(arg) do
if type(v) == "string" then --If the arg is text send a string
net.WriteString(v)
elseif type(v) == "table" then --If the arg is a table(color value), send RGB value(Not sending alpha)
net.WriteUInt(v.r, 8)
net.WriteUInt(v.g, 8)
net.WriteUInt(v.b, 8)
end
end
net.Broadcast()
end
--Clientside
net.Receive("chat", function()
local argc = net.ReadUInt(16) --Number of arguments
local args = {}
for i = 1, argc/2, 1 do --For every 2 arguments insert a color value and string into the chat table
local col = Color(net.ReadUInt(8), net.ReadUInt(8), net.ReadUInt(8), 255)
table.insert(args, col)
table.insert(args, net.ReadString())
end
chat.AddText( unpack(args) )
chat.PlaySound()
end)[/code]
Pretty much a net library version of [url=https://facepunch.com/showthread.php?t=768062]this[/url]
Sorry, you need to Log In to post a reply to this thread.