Jump to content

AntiSpam - Returns


.:HyPeX:.

Recommended Posts

Hi guys, i'm trying to make my antispam a little yet more advanced, but i'm finding problems to work this out (no idea how to properly use returns), can someone tell me how to use it properly?

  
antiSpam = {} 
antiSpamTimer = {} 
function AntiSpam (message, messageType) 
     if messageType == 0 and isTimer(antiSpam[source]) then  
        if isTimer(antiSpamTimer[source]) then 
        killTimer(antiSpamTimer[source]) 
        cancelEvent () 
        antiSpam[source] = setTimer(function(source) antiSpam[source] = nil end, 300000, 1, source) 
        else 
        return 
        cancelEvent () 
        killTimer(antiSpam[source]) 
        antiSpam[source] = setTimer(function(source) antiSpam[source] = nil end, 10000, 1, source) 
        outputChatBox("[AntiSpam]: Stop Flooding, Every new messange will restart your time muted!", source, 0, 170, 255) 
        outputChatBox("[AntiSpam]: Type /sleft to get your time left muted.", source, 0, 170, 255) 
        antiSpamTimer[source] = setTimer(function(source) antiSpamTimer[source] = nil end, 120000, 1, source) 
            else 
            antiSpam[source] = setTimer(function(source) antiSpam[source] = nil end, 1000, 1, source) 
            end 
        end 
     end 
addEventHandler("onPlayerChat", getRootElement(), AntiSpam)   

Link to comment

"return" alone , jump's back to the parent , so the code after it will not be runned.

"return" with an argument like : "return number" , will jump back with the argument. Like data = functionReturn() will hold the data from the argument witch was in the return , in this case the variable "number"

Link to comment

Type 1

  
function getNumber() 
  local number = functionReturn() 
  outputChatBox(number) 
end 
  
function functionReturn() 
  return 5 -- this will return argument "5" to his parent in this case is "function getNumber()" and it will outputChatBox 5 . 
end 
  
  

Type 2

  
  
function Nothing() 
  
--code blablabla 
  
return -- in this case the parent is "nil" bk this function was not called from another function like in "Type 1" so this will return to the RootNode witch is actually the game , so this will just stop executing the code after the "return"  
  
end 
  
  
  

Link to comment

i got some short questions:

1#: why it is number 5?

2#: i can make it just return to the if, or when returning to the function it will ignore the return and follow? lets say on my script, i want to return just like ignoring the last if (lets say it should return to "if messageType == 0..")

function AntiSpam (message, messageType) 
     if messageType == 0 and isTimer(antiSpam[source]) then 
        if isTimer(antiSpamTimer[source]) then 
        killTimer(antiSpamTimer[source]) 
        cancelEvent () 
        antiSpam[source] = setTimer(function(source) antiSpam[source] = nil end, 300000, 1, source) 
        else 
        return 

Link to comment

1. number "5" is just an exampel , "return" can return everything from a number , tabel , string , ......

2. nope it's not possible "return" alone like in your code will end the whole "AntiSpam" function , it will not return to "if messageType == 0..".

"return" can return arguments only for another function from where the current function was called like i showed to you , and not to the function is in it.

I think you miss understand "return" function.

"return" function can return only arguments stored like :

tableA = {}

return tableA

local number = "5"

return number

local stringA = "Test"

return stringA

and not to commands like "if"

Link to comment
There always is. How do you expect the function bound to the event to be called?

i'm not sure how to translate that part with the bound, but i was thinking of 2 separate functions, and like return to the function before if that doesnt works and it then should run correctly... (lets say it would trigger the 1st one as the 2nd one wasnt runing). But then it comes to the part, how i call it? as you said return doesnt do it. hows a caller, as you said?

Link to comment

An event can be bound to the function, the function handles the event. The event name is pretty much like an identifier to the handler when you're triggering it.

An example of how return works:

local function main() 
    local received = MessageHandler:GetSingleton():GetMessage() 
    while ( true ) do 
        if ( received ) then 
            if ( received == MESSAGE_QUIT ) then 
                -- this will halt the function and return to the caller 
                -- in this example, this is the entry point, and the caller should have been the host program (the one who runs Lua, e.g. MTA) 
                return 
            end 
             
            MessageHandler:GetSingleton():HandleMessage(received) 
            received = MessageHandler:GetSingleton():GetMessage() 
        end 
    end 
end 
  
... 
function MessageHandler:GetMessage() 
    -- some code here to get the message 
    -- now you return the message, this returns to the caller with the message 
    -- if you check the function 'main' above, you'll see it expects the function to return that message 
    return message 
end 

All of this doesn't really make much sense (the main function could have been done in a more logical way but it would not make use of the return keyword which was the point of this example), but it should have been enough to understand this.

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...