zocken212 Posted July 19, 2013 Posted July 19, 2013 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())
Moderators IIYAMA Posted July 19, 2013 Moderators Posted July 19, 2013 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) Do you want to improve your Lua programming skills and make less mistakes? Start with Lua Language Server! Useful functions 3x Spoiler checkPassiveTimer getScreenStartPositionFromBox getPedGender Tutorials 4x Spoiler Scaling DX Events Attach an addEventHandler on a group of elements Debugging
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now