General > Developers Corner

Tags

<< < (4/5) > >>

easer7:
Quick question about the else/if part though.


if (SERVER) then
        AddCSLuaFile("autorun/tags.lua")
end
 
if (CLIENT) then
function Insert( pl, msg )
                local tab = {}
        if pl:IsUserGroup("headmanagement") then
                table.insert( tab, Color( 255, 0, 0, 255 ) )
                        table.insert( tab, "[Head Management] " )
                table.insert( tab, Color( 255, 255, 255, 255 ) )
                        table.insert( tab, pl:Nick() )
                        table.insert( tab, ": " )
                table.insert( tab, msg )
        elseif pl:IsUserGroup("leadmanagement") then
                table.insert( tab, Color( 255, 0, 0, 255 ) )
                        table.insert( tab, "[Lead Management] " )
                table.insert( tab, Color( 255, 255, 255, 255 ) )
                        table.insert( tab, pl:Nick() )
                        table.insert( tab, ": " )
                table.insert( tab, msg )
        elseif pl:IsUserGroup("management") then
                table.insert( tab, Color( 255, 0, 0, 255 ) )
                        table.insert( tab, "[Management] " )
                table.insert( tab, Color( 255, 255, 255, 255 ) )
                        table.insert( tab, pl:Nick() )
                        table.insert( tab, ": " )
                table.insert( tab, msg )
        elseif pl:IsUserGroup("leadmoderator") then
                table.insert( tab, Color( 33, 110, 252, 255 ) )
                        table.insert( tab, "[Lead Moderator] " )
                table.insert( tab, Color( 255, 255, 255, 255 ) )
                        table.insert( tab, pl:Nick() )
                        table.insert( tab, ": " )
                table.insert( tab, msg )
        elseif pl:IsUserGroup("moderator") then
                table.insert( tab, Color( 33, 110, 252, 255 ) )
                        table.insert( tab, "[Moderator] " )
                table.insert( tab, Color( 255, 255, 255, 255 ) )
                        table.insert( tab, pl:Nick() )
                        table.insert( tab, ": " )
                table.insert( tab, msg )
        elseif pl:IsUserGroup("premium") then
                table.insert( tab, Color( 245, 226, 20, 255 ) )
                        table.insert( tab, "[Premium] " )
                table.insert( tab, Color( 255, 255, 255, 255 ) )
                        table.insert( tab, pl:Nick() )
                        table.insert( tab, ": " )
                table.insert( tab, msg )
        elseif pl:IsUserGroup("trusted") then
                table.insert( tab, Color( 194, 19, 153, 255 ) )
                        table.insert( tab, "[Trusted] " )
                table.insert( tab, Color( 255, 255, 255, 255 ) )
                        table.insert( tab, pl:Nick() )
                        table.insert( tab, ": " )
                table.insert( tab, msg )
        elseif pl:IsUserGroup("supporter") then
                table.insert( tab, Color( 86, 185, 80, 255 ) )
                        table.insert( tab, "[Supporter] " )
                table.insert( tab, Color( 255, 255, 255, 255 ) )
                        table.insert( tab, pl:Nick() )
                        table.insert( tab, ": " )
                table.insert( tab, msg )
        else
                table.insert( tab, Color( 86, 185, 80, 255 ) )
                        table.insert( tab, "[User] " )
                table.insert( tab, Color( 255, 255, 255, 255 ) )
                        table.insert( tab, pl:Nick() )
                        table.insert( tab, ": " )                     
                table.insert( tab, msg )
        end
chat.AddText( unpack(tab) )
return true
        end
end
hook.Add("OnPlayerChat", "InsertTags", Insert)


I'm getting the error: /tags.lua.lua:0
[ERROR] lua/autorun/tags.lua.lua:8: unfinished string near '"headmanagement) then'  1. unknown - lua/autorun/tags.
What is this?

MrPresident:
Try this...

btw.. do you use something like Notepad++ to edit your lua? Your tabulation is atrocious. lol


--- Code: ---if (SERVER) then
AddCSLuaFile("autorun/tags.lua")
end

if (CLIENT) then
function Insert( pl, msg )
local tab = {}
if pl:IsUserGroup("headmanagement") then
table.insert( tab, Color( 255, 0, 0, 255 ) )
table.insert( tab, "[Head Management] " )
table.insert( tab, Color( 255, 255, 255, 255 ) )
table.insert( tab, pl:Nick() )
table.insert( tab, ": " )
table.insert( tab, msg )
elseif pl:IsUserGroup("leadmanagement") then
table.insert( tab, Color( 255, 0, 0, 255 ) )
table.insert( tab, "[Lead Management] " )
table.insert( tab, Color( 255, 255, 255, 255 ) )
table.insert( tab, pl:Nick() )
table.insert( tab, ": " )
table.insert( tab, msg )
elseif pl:IsUserGroup("management") then
table.insert( tab, Color( 255, 0, 0, 255 ) )
table.insert( tab, "[Management] " )
table.insert( tab, Color( 255, 255, 255, 255 ) )
table.insert( tab, pl:Nick() )
table.insert( tab, ": " )
table.insert( tab, msg )
elseif pl:IsUserGroup("leadmoderator") then
table.insert( tab, Color( 33, 110, 252, 255 ) )
table.insert( tab, "[Lead Moderator] " )
table.insert( tab, Color( 255, 255, 255, 255 ) )
table.insert( tab, pl:Nick() )
table.insert( tab, ": " )
table.insert( tab, msg )
elseif pl:IsUserGroup("moderator") then
table.insert( tab, Color( 33, 110, 252, 255 ) )
table.insert( tab, "[Moderator] " )
table.insert( tab, Color( 255, 255, 255, 255 ) )
table.insert( tab, pl:Nick() )
table.insert( tab, ": " )
table.insert( tab, msg )
elseif pl:IsUserGroup("premium") then
table.insert( tab, Color( 245, 226, 20, 255 ) )
table.insert( tab, "[Premium] " )
table.insert( tab, Color( 255, 255, 255, 255 ) )
table.insert( tab, pl:Nick() )
table.insert( tab, ": " )
table.insert( tab, msg )
elseif pl:IsUserGroup("trusted") then
table.insert( tab, Color( 194, 19, 153, 255 ) )
table.insert( tab, "[Trusted] " )
table.insert( tab, Color( 255, 255, 255, 255 ) )
table.insert( tab, pl:Nick() )
table.insert( tab, ": " )
table.insert( tab, msg )
elseif pl:IsUserGroup("supporter") then
table.insert( tab, Color( 86, 185, 80, 255 ) )
table.insert( tab, "[Supporter] " )
table.insert( tab, Color( 255, 255, 255, 255 ) )
table.insert( tab, pl:Nick() )
table.insert( tab, ": " )
table.insert( tab, msg )
else
table.insert( tab, Color( 86, 185, 80, 255 ) )
table.insert( tab, "[User] " )
table.insert( tab, Color( 255, 255, 255, 255 ) )
table.insert( tab, pl:Nick() )
table.insert( tab, ": " )                     
table.insert( tab, msg )
end
chat.AddText( unpack(tab) )
return true
end
hook.Add("OnPlayerChat", "InsertTags", Insert)
end

--- End code ---

easer7:
I do use notepad++ =( I thought notepad++ was of the highest quality, what should I use?

JamminR:

--- Quote from: easer7 on January 08, 2013, 05:04:37 PM ---I do use notepad++ =( I thought notepad++ was of the highest quality, what should I use?

--- End quote ---
Notepad++ is fine. Just keep your lines together.
Also, in case you missed what MrPresident did, from your error, your missing the closing quotation mark.
'"headmanagement) then' should have been  '"headmanagement") then'
Also, why use so many table.inserts?
Table manipulation is time consuming (for any programming language, even Lua).
It would be better, optimization wise, to use this ...

--- Code: ---elseif pl:IsUserGroup("management") then
table.insert( tab, Color( 255, 0, 0, 255 ) .. "[Management] " .. Color( 255, 255, 255, 255 ) .. pl:Nick() .. ": " .. msg )
elseif ...

--- End code ---
than what you're doing currently.
Look up "lua concatenation".
Those ".." bring together many string items.

Megiddo:

--- Quote from: JamminR on January 08, 2013, 08:12:07 PM ---Also, why use so many table.inserts?
Table manipulation is time consuming (for any programming language, even Lua).
It would be better, optimization wise, to use this ...

--- Code: ---elseif pl:IsUserGroup("management") then
table.insert( tab, Color( 255, 0, 0, 255 ) .. "[Management] " .. Color( 255, 255, 255, 255 ) .. pl:Nick() .. ": " .. msg )
elseif ...

--- End code ---
than what you're doing currently.
Look up "lua concatenation".
Those ".." bring together many string items.

--- End quote ---

It's necessary to do it the way he currently is. The color objects are not strings, and chat.AddText wants a list of string or color objects.

Navigation

[0] Message Index

[#] Next page

[*] Previous page

Go to full version