Jump to content

ipairs problem


Recommended Posts

Hey.

I have a problem with ipairs: It should look up if there is already one element with a specific data. As long as only one player is on the server it works fine but when 2 are there, it always takes an other element as I want ...

addEvent("checkPresidentServer",true) 
addEventHandler("checkPresidentServer",getRootElement(), 
function () 
    for index, p in ipairs(getElementsByType("player")) do 
        local element = getElementData(p,"president") 
        if element == "True"  then 
            triggerClientEvent(source,"setPresidentToFalse",getRootElement()) 
        elseif element == "False" then 
            triggerClientEvent(source,"setPresidentToTrue",getRootElement()) 
        end 
    end 
end) 

It's always set to "setPresidentToTrue", also when there is a president..

EDIT: It's working right now, I placed a "return" below triggerClientEvent(source,"setPresidentToFalse",getRootElement())

Link to comment
  • Moderators
EDIT: It's working right now, I placed a "return" below triggerClientEvent(source,"setPresidentToFalse",getRootElement())

Then the loop will end and you don't know if there is a president.

Use break instead:

  
    addEvent("checkPresidentServer",true) 
    addEventHandler("checkPresidentServer",getRootElement(), 
    function () 
        local presidentFound = false 
        for index, player in pairs(getElementsByType("player")) do  
            if getElementData(player,"president") == "True"  then 
                triggerClientEvent(source,"setPresidentToFalse",getRootElement()) 
                presidentFound = true 
                break 
            end 
        end 
        if not presidentFound then 
            triggerClientEvent(source,"setPresidentToTrue",getRootElement()) 
        end 
    end) 

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