Jump to content

[QUESTION]Tables


Agon

Recommended Posts

Hi guys. This is my chat script (in freeroam/fr_server.lua).

addEventHandler('onPlayerChat', g_Root, 
    function(msg, type) 
        if type == 0 then 
            cancelEvent() 
                local r, g, b = getPlayerNametagColor(source) 
                local accName = getAccountName ( getPlayerAccount ( source ) ) 
            if isObjectInACLGroup ("user."..accName, aclGetGroup ( "Owner" ) ) then 
                outputChatBox("#00A6FF[Owner]" .. getPlayerName(source) .. ': #FFFFFF' .. msg:gsub('#%x%x%x%x%x%x', ''), g_Root, r, g, b, true) 
                outputServerLog( "CHAT: " .. getPlayerName(source) .. ": " .. msg ) 
            else if isObjectInACLGroup ("user."..accName, aclGetGroup ( "Admin" ) ) then 
                outputChatBox("#FF0000[Admin]" .. getPlayerName(source) .. ': #FFFFFF' .. msg:gsub('#%x%x%x%x%x%x', ''), g_Root, r, g, b, true) 
                outputServerLog( "CHAT: " .. getPlayerName(source) .. ": " .. msg ) 
            else if isObjectInACLGroup ("user."..accName, aclGetGroup ( "Moderator" ) ) then 
                outputChatBox("#FFCC00[Moderator]" .. getPlayerName(source) .. ': #FFFFFF' .. msg:gsub('#%x%x%x%x%x%x', ''), g_Root, r, g, b, true) 
                outputServerLog( "CHAT: " .. getPlayerName(source) .. ": " .. msg ) 
            else outputChatBox( getPlayerName(source) .. ': #FFFFFF' .. msg:gsub('#%x%x%x%x%x%x', ''), g_Root, r, g, b, true) 
                outputServerLog( "CHAT: " .. getPlayerName(source) .. ": " .. msg ) 
            end 
        end 
    end 
end 
end 
) 

as you know this thingy changes hex codes to space => msg:gsub('#%x%x%x%x%x%x', '')

for example when I type #FF0000Hello #00FF00World, it should output Hello World but it outputs, Hello World. I want to change some words to anothers.

For example when someone types "lol", it should change it to "duck", "bastard" to "***" and "account" to "123". How can i make this with tables? i tried this but I don't think it will work:

originalMessages = {lol, bastard, account} 
newMessages = {duck, ***, 123} 
msg:gsub(tostring(originalMessages), tostring(newMessages)) 

or should I make it like this?

originalMessages = {[lol], [bastard], [account]} 
newMessages = {[duck], [***], [123]} 

I don't know tables very well so i need your helps. Thanks in advance.

Link to comment
Guest Guest4401

I wouldn't choose indexed tables for this. Try out something like this:

t = { 
    lol = "duck", 
    bastard = "***" 
} 
  
msg = "Haha look at that bastard. lol at him." 
  
for k,v in pairs(t) do 
    msg = msg:gsub(k,v) 
end 
  
print(msg) -- Haha look at that ***. duck at him. 

Link to comment
local BadWords = { 
    ["xyu"] = "***"; 
    ["lol"] = "123"; 
} 
  
function blockChatMessage(message) 
    cancelEvent() 
    for word, text in pairs(BadWords) do 
        msg = message:gsub(" "..word.." ", " "..text.." ") 
    end 
    outputChatBox( "CHAT: " .. getPlayerName(source) .. ": " .. msg, root ) 
end 
addEventHandler( "onPlayerChat", getRootElement(), blockChatMessage ) 

Link to comment
Guest Guest4401

@Flaker

In your code the message won't parse colorcodes, and also it would reveal private teamchat message to publicity, and won't log the messages.

Try this:

t = { 
    lol = "duck", 
    bastard = "***" 
} 
  
addEventHandler("onPlayerChat",root, 
    function(msg,type) 
        local _msg = msg 
        local msg = msg 
        if type == 0 then -- if it's not in teamchat or displayed by /me 
            cancelEvent() 
            for k,v in pairs(t) do -- loop through all bad words 
                msg = msg:gsub(k,v) -- replace each one of them 
            end 
            outputChatBox(getPlayerName(source)..":#ffffff "..msg,root,255,255,255,true) -- output the editted message in the chatbox 
            outputServerLog("CHAT: "..getPlayerName(source)..": ".._msg) -- output the original message in the logs 
        end 
    end 
) 

Link to comment

wow it works :D thank you guys :) and karthik_184 thank you for explaining the script :)

and 1 more question. How can i detect player's message? I want to make a bot system. for example when i say hello, the bot will say hello. I know how to make a bot talk but don't know how to detect a message :)

Link to comment
Guest Guest4401

You mean if I type "hello" in the chatbox, the bot should say hello back to me? If so:

addEventHandler("onPlayerChat",root, 
    function(msg,t_) 
        if msg:lower() == "hello" and t_ == 0 then 
            setTimer(outputChatBox,100,1,"BOT: Hello "..getPlayerName(source):gsub("#%x%x%x%x%x%x","")..". Nice to meet you!",source,255,0,0,true) 
        end 
    end 
) 

Link to comment

thank you ernst and karthik_184 but i have a problem now :D

t = { lol = "duck" 
       bastard = "***" 
} --- this code works 

BUT

t = { lol = "duck" 
       bastard = "***" 
       #%x%x%x%x%x%x = "" 
} --- this code doesn't work 

I tried making even this:

t = { "lol" = "duck" 
       "bastard" = "***" 
       "#%x%x%x%x%x%x" = "" 
} --- but this code doesn't work either 

so is there any way of disabling color codes on messages or do you have an idea about this problem? :)

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...