• What's wrong with this chat script?
    9 replies, posted
A friend of mine created this basic chat system for our sandbox RP yet I can't seem to get it working. It's in lua/autorun. [lua] if (gamemode.Get("sandbox") then Molotov = {}; Molotov.Commands = {}; Molotov.Config = {}; Molotov.Config["Enabled"] = true; Molotov.Config["RangedChatTalkRange"] = 256; if (not Molotov.Config["Enabled"]) then return; end; function Molotov.AddCommand(name, callback) table.insert(Molotov.Commands, { name = name, callback = callback }); end; function Molotov.OutOfCharacterChat(player, arguments) if (argumments == "") then return ""; end; player:ChatPrint("(OOC) " .. player:Name() .. ":" .. arguments); player:PrintMessage(2, "(OOC) " .. player:Name() .. ":" .. arguments); return ""; end; Molotov.AddCommand("//", Molotov.OutOfCharacterChat); Molotov.AddCommand("/ooc", Molotov.OutOfCharacterChat); function Molotov.RangedChat(message, position, size) local ents = ents.FindInSphere(position, size); for k, v in pairs (ents) do if (v:IsPlayer()) then v:ChatPrint(message); end; end; end; function Molotov.RangedChatPlayerSay(player, text) for k, v in pairs(Molotov.Commands) do if (v.name = string.Explode(" ", string.lower(text))[1]) then return v.callback(player, "" .. string.sub(text, string.len(v.name) + 2, string.len(text))); end; end; if (Molotov.Config["Ranged Chat"] == 1) then Molotov.RangedChat(player:Name() .. ": " .. text, player:GetPos(), Molotov.Config["RangedChatTalkRange"]); return ""; else return text; end; end; hook.Add("PlayerSay", "RangedChatPlayerSay", Molotov.RangedChatPlayerSay); else return; end; [/lua]
[lua] if (gamemode.Get("sandbox")) then Molotov = {}; Molotov.Commands = {}; Molotov.Config = {}; Molotov.Config["Enabled"] = true; Molotov.Config["RangedChatTalkRange"] = 256; if (not Molotov.Config["Enabled"]) then return; end; function Molotov.AddCommand(name, callback) table.insert(Molotov.Commands, { name = name, callback = callback }); end; function Molotov.OutOfCharacterChat(player, arguments) if (argumments == "") then return ""; end; player:ChatPrint("(OOC) " .. player:Name() .. ":" .. arguments); player:PrintMessage(2, "(OOC) " .. player:Name() .. ":" .. arguments); return ""; end; Molotov.AddCommand("//", Molotov.OutOfCharacterChat); Molotov.AddCommand("/ooc", Molotov.OutOfCharacterChat); function Molotov.RangedChat(message, position, size) local ents = ents.FindInSphere(position, size); for k, v in pairs (ents) do if (v:IsPlayer()) then v:ChatPrint(message); end; end; end; function Molotov.RangedChatPlayerSay(player, text) for k, v in pairs(Molotov.Commands) do if (v.name == string.Explode(" ", string.lower(text))[1]) then return v.callback(player, "" .. string.sub(text, string.len(v.name) + 2, string.len(text))); end; end; if (Molotov.Config["Ranged Chat"] == 1) then Molotov.RangedChat(player:Name() .. ": " .. text, player:GetPos(), Molotov.Config["RangedChatTalkRange"]); return ""; else return text; end; end; hook.Add("PlayerSay", "RangedChatPlayerSay", Molotov.RangedChatPlayerSay); else return; end; [/lua] syntax errors at line 1 and 43 line 1 was missing an ) line 43 was supposed to be == instead of = Fixed it for you ^
Thanks CapsAdmin!
[lua]if (gamemode.Get("sandbox")) then Molotov = {}; Molotov.Commands = {}; Molotov.Config = {}; Molotov.Config["Enabled"] = true; Molotov.Config["RangedChatTalkRange"] = 256; if (not Molotov.Config["Enabled"]) then return; end; function Molotov.AddCommand(name, callback) table.insert(Molotov.Commands, { name = name, callback = callback }); end; function Molotov.OutOfCharacterChat(ply, arguments) if (argumments == "") then return ""; end; for _, v in pairs(player.GetAll()) do v:ChatPrint("(OOC) " .. ply:Name() .. ":" .. arguments); v:PrintMessage(2, "(OOC) " .. ply:Name() .. ":" .. arguments); end; return ""; end; Molotov.AddCommand("//", Molotov.OutOfCharacterChat); Molotov.AddCommand("/ooc", Molotov.OutOfCharacterChat); function Molotov.RangedChat(message, position, size) local ents = ents.FindInSphere(position, size); for k, v in pairs (ents) do if (v:IsPlayer()) then v:ChatPrint(message); end; end; end; function Molotov.RangedChatPlayerSay(player, text) for k, v in pairs(Molotov.Commands) do if (v.name == string.Explode(" ", string.lower(text))[1]) then return v.callback(player, "" .. string.sub(text, string.len(v.name) + 2, string.len(text))); end; end; if (Molotov.Config["Ranged Chat"] == 1) then Molotov.RangedChat(player:Name() .. ": " .. text, player:GetPos(), Molotov.Config["RangedChatTalkRange"]); return ""; else return text; end; end; hook.Add("PlayerSay", "RangedChatPlayerSay", Molotov.RangedChatPlayerSay); else return; end; [/lua] Fixed OOC Chat
You should be using ipairs instead of pairs on the player loops, it's faster on numerically indexed tables. (player.GetAll())
[QUOTE=NullPoint;19442333]You should be using ipairs instead of pairs on the player loops, it's faster on numerically indexed tables. (player.GetAll())[/QUOTE] whats the hole diffrence between pairs and ipairs?
The ipairs iterator traverses a table increasing a variable by one each call and indexing the table with it. pairs traverses a table using the next function which returns a random key value pair that it hasn't returned already until all have been returned.
Interesting, i'll keep that in mind next time i do a loop.
[QUOTE=NullPoint;19442585]The ipairs iterator traverses a table increasing a variable by one each call and indexing the table with it. pairs traverses a table using the next function which returns a random key value pair that it hasn't returned already until all have been returned.[/QUOTE] Correct, but I wouldn't worry which way you did it in a say hook. But in a think or draw hook, use ipairs as much as possible, as the difference may be significant.
Sorry, you need to Log In to post a reply to this thread.