Jump to content

Endless loop


FSXTim

Recommended Posts

Hay,

I'm trying to raise the playtime (Spielzeit) every 60 seconds about 1.

So I created a endless loop, but it isn't endless. After two times, it stops.

function setZeit() 
    for index, spieler in ipairs (getElementsByType("player")) do 
        if getElementData( spieler, "LoggedIn" ) then 
            setElementData(spieler, "Spielzeit", getElementData(spieler, "Spielzeit") + 1) 
            local result =  mysql_query(sqlcon, "UPDATE user_data SET Spielzeit='"..tostring(getElementData(spieler,"Spielzeit")).."' WHERE Username='"..tostring(getElementData(spieler,"Username")).."';") 
            if (result) then mysql_free_result(result) end 
        end 
    end 
    setTimer(setZeit2, 30000, 1)     
end 
addEventHandler("onResourceStart", getResourceRootElement(getThisResource()), setZeit) 
  
function setZeit2() 
    setTimer(setZeit, 30000, 1) 
end 

Greets

Link to comment
  • Other Languages Moderators

The timers look fine, try add outputChatBox inside function setZeit, and see if it does output two times and then stop or not.

Edited by Guest
Link to comment

Try this.

function setZeit() 
    for index, spieler in ipairs (getElementsByType("player")) do 
        if getElementData( spieler, "LoggedIn" ) then 
            setElementData(spieler, "Spielzeit", getElementData(spieler, "Spielzeit") + 1) 
            local result =  mysql_query(sqlcon, "UPDATE user_data SET Spielzeit='"..tostring(getElementData(spieler,"Spielzeit")).."' WHERE Username='"..tostring(getElementData(spieler,"Username")).."';") 
            if (result) then mysql_free_result(result) end 
        end 
    end 
end 
addEventHandler("onResourceStart", getResourceRootElement(getThisResource()), setZeit2) 
  
function setZeit2() 
    setTimer(setZeit, 60000, 0) 
end 

Link to comment

@Alan.Alexander: This doesn't work.

@TAPL

function setZeit() 
    outputChatBox("Test1", getRootElement()) 
    for index, spieler in ipairs (getElementsByType("player")) do 
        outputChatBox("Test2", getRootElement()) 
        if getElementData( spieler, "LoggedIn" ) then 
            outputChatBox("Test3", getRootElement()) 
            setElementData(spieler, "Spielzeit", getElementData(spieler, "Spielzeit") + 1) 
            local result =  mysql_query(sqlcon, "UPDATE user_data SET Spielzeit='"..tostring(getElementData(spieler,"Spielzeit")).."' WHERE Username='"..tostring(getElementData(spieler,"Username")).."';") 
            if (result) then mysql_free_result(result) end 
        end 
    end 
    setTimer(setZeit2, 30000, 1)     
end 
addEventHandler("onResourceStart", getResourceRootElement(getThisResource()), setZeit) 
  
function setZeit2() 
    setTimer(setZeit, 30000, 1) 
end 

The ChatBox outputs the following two times:

gadar2ul.jpg

Greets

Link to comment

If I am alone on my server, this script works perfectly. If another player join, the loop stops, but why?

  
function setZeit() 
    outputChatBox("Test1", getRootElement()) 
    for index, spieler in ipairs (getElementsByType("player")) do 
        outputChatBox("Test2", getRootElement()) 
        if getElementData( spieler, "LoggedIn" ) then 
            outputChatBox("Test3", getRootElement()) 
            setElementData(spieler, "Spielzeit", getElementData(spieler, "Spielzeit") + 1) 
            local result =  mysql_query(sqlcon, "UPDATE user_data SET Spielzeit='"..tostring(getElementData(spieler,"Spielzeit")).."' WHERE Username='"..tostring(getElementData(spieler,"Username")).."';") 
            if (result) then mysql_free_result(result) end 
        end 
    end 
    setTimer(setZeit2, 30000, 1)     
end 
addEventHandler("onResourceStart", getResourceRootElement(getThisResource()), setZeit) 
  
function setZeit2() 
    setTimer(setZeit, 30000, 1) 
end 

Greets

Link to comment

Why don't you use a timer only?

Also, I have noticed that your third argument on both setTimers is 1. That means, it will be executed only ONCE. Set 0 instead.

function setZeit() 
     setTimer( 
     function() 
     outputChatBox("Test1", getRootElement()) 
    for index, spieler in ipairs (getElementsByType("player")) do 
        outputChatBox("Test2", getRootElement()) 
        if getElementData( spieler, "LoggedIn" ) then 
            outputChatBox("Test3", getRootElement()) 
            setElementData(spieler, "Spielzeit", getElementData(spieler, "Spielzeit") + 1) 
            local result =  mysql_query(sqlcon, "UPDATE user_data SET Spielzeit='"..tostring(getElementData(spieler,"Spielzeit")).."' WHERE Username='"..tostring(getElementData(spieler,"Username")).."';") 
            if (result) then mysql_free_result(result) end 
        end 
    end 
     end, 60000, 0) 
end 
  
addEventHandler("onResourceStart", getResourceRootElement(getThisResource()), setZeit) 

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