Jump to content

[HELP] MissionTimer


Backsage

Recommended Posts

Hello guys. I'm interested in using mission timer. However, there is an issue.

addEventHandler("onClientResourceStart", root, 
    function() 
        ATimer = exports.missiontimer:createMissionTimer (5000,true,"%m:%s",0.5,20,true,"default-bold",1,255,255,255)        
    end 
) 
  
addEventHandler("onClientMissionTimerElapsed", ATimer, 
    function() 
        destroyElement(ATimer) 
    end 
) 
  

[2016-02-13 22:32:09] WARNING: test\timer.lua:7: Bad argument @ 'addEventHandler' [Expected element at argument 2, got nil]

It's not working for the variable ATimer. For some reason, it's only working when I do this:

  
addEventHandler("onClientMissionTimerElapsed", root, 
    function() 
        destroyElement(ATimer) 
    end 
) 

I would like to know why this is only working for root and not something else.

Edit: And I would also like to know how to make a mission timer appear on another client's screen with the time synced.

Edited by Guest
Link to comment
Hello guys. I'm interested in using mission timer. However, there is an issue.
  
addEventHandler("onClientResourceStart", root, 
    function() 
        ATimer = exports.missiontimer:createMissionTimer (5000,true,"%m:%s",0.5,20,true,"default-bold",1,255,255,255)        
    end 
) 
  
addEventHandler("onClientMissionTimerElapsed", ATimer, 
    function() 
        destroyElement(ATimer) 
    end 
) 
  

[2016-02-13 22:32:09] WARNING: test\timer.lua:7: Bad argument @ 'addEventHandler' [Expected element at argument 2, got nil]

It's not working for the variable ATimer. For some reason, it's only working when I do this:

  
addEventHandler("onClientMissionTimerElapsed", root, 
    function() 
        destroyElement(ATimer) 
    end 
) 
  

I would like to know why this is only working for root and not something else.

Edit: And I would also like to know how to make a mission timer appear on another client's screen with the time synced.

addEventHandler 

attachedTo: The element you wish to attach the handler to. The handler will only be called when the event it is attached to is triggered for this element, or one of its children. Often, this can be the root element (meaning the handler will be called when the event is triggered for any element).

Link to comment

When using this code:

addEventHandler("onResourceStart", root, 
    function() 
        test = exports.missiontimer:createMissionTimer (5000,true,"%m:%s",0.5,20,true,"default-bold",1,255,255,255) 
    end 
) 
  
  
addEventHandler("onMissionTimerElapsed", root, 
    function() 
        destroyElement(test) 
    end 
) 
  
addCommandHandler("tdmdt", 
    function () 
        if ( test ) then 
            destroyElement(test) 
        end 
    end 
) 

I get these errors. Client side:

[2016-02-14 16:38:37] ERROR: missiontimer\missiontimer_client.lua:112: attempt to index field '?' (a nil value)

[2016-02-14 16:38:42] WARNING: missiontimer\missiontimer_client.lua:45: Bad argument @ 'triggerEvent' [Expected element at argument 2]

And when I try using the /tdmdt to destroy the server, I get this. Server side:

[2016-02-14 16:46:40] WARNING: test\timer2.lua:17: Bad argument @ 'destroyElement' [Expected element at argument 1]

Why is missiontimer bugging for me?

missiontimer_client.lua Client side code:

rootElement = getRootElement() 
thisResource = getThisResource() 
missionTimers = {} 
bool = { [false]=true, [true]=true } 
addEvent ( "onClientMissionTimerElapsed", true ) 
  
addEventHandler("onClientResourceStart",resourceRoot, 
    function() 
        triggerServerEvent ( "onClientMissionTimerDownloaded", getLocalPlayer() ) 
    end 
) 
  
addEventHandler ("onClientResourceStop",rootElement, 
    function() 
        for i,timer in ipairs(getElementsByType("missiontimer",source)) do 
            destroyElement(timer) 
        end 
    end 
) 
  
function createMissionTimer ( duration, countdown, timerFormat, x, y, bg, font, scale, r, g, b ) 
    sourceResource = sourceResource or thisResource 
    local element = createElement ( "missiontimer" ) 
    setElementParent ( element, getResourceDynamicElementRoot(sourceResource) ) 
    setupMissionTimer ( element, duration, countdown, timerFormat, x, y, bg, font, scale, r, g, b ) 
    return element 
end 
  
function setupMissionTimer ( element, duration, countdown, timerFormat, x, y, bg, font, scale, r, g, b ) 
    if missionTimers[element] then return end 
    addEventHandler ( "onClientElementDestroy", element, onMissionTimerDestroy ) 
    missionTimers[element] = {} 
    missionTimers[element].x = tonumber(x) or 0 
    missionTimers[element].y = tonumber(y) or 0 
    missionTimers[element].countdown = countdown 
    missionTimers[element].duration = duration 
    missionTimers[element].originalTick = getTickCount() 
    missionTimers[element].timerFormat = type( timerFormat ) == "string" and timerFormat or "%m:%s:%cs" 
    missionTimers[element].bg = (bool[bg] == nil and true) or bg 
    missionTimers[element].font = font or "default-bold" 
    missionTimers[element].scale = tonumber(scale) or 1 
    missionTimers[element].hurrytime = 15000 
    missionTimers[element].formatWidth = dxGetTextWidth(missionTimers[element].timerFormat, missionTimers[element].scale, missionTimers[element].font) 
    missionTimers[element].colour = (r and g and b) and tocolor(r, g, b) or tocolor(255,255,255) 
    missionTimers[element].timer = setTimer ( triggerEvent, duration, 1, "onClientMissionTimerElapsed", element ) 
end 
  
function setMissionTimerTime ( timer, time ) 
    if missionTimers[timer] then 
        missionTimers[timer].duration = tonumber(time) or missionTimers[timer].remaining 
        missionTimers[timer].originalTick = getTickCount() 
         
        if isTimer( missionTimers[timer].timer ) then 
            killTimer ( missionTimers[timer].timer ) 
        end 
        missionTimers[timer].timer = setTimer ( triggerEvent, missionTimers[timer].duration, 1, "onClientMissionTimerElapsed", element )             
             
        return true 
    end 
    return false 
end 
  
function getMissionTimerTime ( timer ) 
    if missionTimers[timer] then 
        if missionTimers[timer].countdown then 
            return math.max(missionTimers[timer].duration - (getTickCount() - missionTimers[timer].originalTick),0) 
        else 
            return (getTickCount() - missionTimers[timer].originalTick) 
        end 
    end 
    return false 
end 
  
function setMissionTimerFrozen ( timer, frozen ) 
    if frozen == not not missionTimers[timer].frozen then return false end 
    if missionTimers[timer] and bool[frozen] then 
        missionTimers[timer].frozen = frozen or nil 
        if frozen then 
            if isTimer( missionTimers[timer].timer ) then 
                killTimer ( missionTimers[timer].timer ) 
            end 
            missionTimers[timer].timer = nil 
            missionTimers[timer].duration = getMissionTimerTime ( timer ) 
        else 
            missionTimers[timer].timer = setTimer ( triggerEvent, missionTimers[timer].duration, 1, "onClientMissionTimerElapsed", timer )       
            missionTimers[timer].originalTick = getTickCount() 
        end 
        return true 
    end 
    return false 
end 
  
function isMissionTimerFrozen ( timer ) 
    return not not missionTimers[timer].frozen 
end 
  
function setMissionTimerHurryTime ( timer, time ) 
    missionTimers[timer].hurrytime = tonumber(time) or 15000 
end 
  
function setMissionTimerFormat( timer, timerFormat ) 
    if type( timerFormat ) ~= "string" then return false end 
     
    if missionTimers[timer] then 
        missionTimers[timer].timerFormat = timerFormat 
        missionTimers[timer].formatWidth = dxGetTextWidth(missionTimers[timer].timerFormat, missionTimers[timer].scale, missionTimers[timer].font) 
    end 
end 
  
function onMissionTimerDestroy() 
    for i,timer in ipairs(getTimers()) do 
        if timer == missionTimers[source].timer then 
            killTimer ( timer ) 
            break 
        end 
    end 
    missionTimers[source] = nil 
end 

Edited by Guest
Link to comment
You posted the wrong wrong section of code there...triggerEvent is no where in the section you posted, need the section that the debugscript is actually talking about.

I was only posting my code, where I try to create a missiontimer. But I guess it seems like missiontimer itself is bugged.

missiontimer_client.lua Client side code:

rootElement = getRootElement() 
thisResource = getThisResource() 
missionTimers = {} 
bool = { [false]=true, [true]=true } 
addEvent ( "onClientMissionTimerElapsed", true ) 
  
addEventHandler("onClientResourceStart",resourceRoot, 
    function() 
        triggerServerEvent ( "onClientMissionTimerDownloaded", getLocalPlayer() ) 
    end 
) 
  
addEventHandler ("onClientResourceStop",rootElement, 
    function() 
        for i,timer in ipairs(getElementsByType("missiontimer",source)) do 
            destroyElement(timer) 
        end 
    end 
) 
  
function createMissionTimer ( duration, countdown, timerFormat, x, y, bg, font, scale, r, g, b ) 
    sourceResource = sourceResource or thisResource 
    local element = createElement ( "missiontimer" ) 
    setElementParent ( element, getResourceDynamicElementRoot(sourceResource) ) 
    setupMissionTimer ( element, duration, countdown, timerFormat, x, y, bg, font, scale, r, g, b ) 
    return element 
end 
  
function setupMissionTimer ( element, duration, countdown, timerFormat, x, y, bg, font, scale, r, g, b ) 
    if missionTimers[element] then return end 
    addEventHandler ( "onClientElementDestroy", element, onMissionTimerDestroy ) 
    missionTimers[element] = {} 
    missionTimers[element].x = tonumber(x) or 0 
    missionTimers[element].y = tonumber(y) or 0 
    missionTimers[element].countdown = countdown 
    missionTimers[element].duration = duration 
    missionTimers[element].originalTick = getTickCount() 
    missionTimers[element].timerFormat = type( timerFormat ) == "string" and timerFormat or "%m:%s:%cs" 
    missionTimers[element].bg = (bool[bg] == nil and true) or bg 
    missionTimers[element].font = font or "default-bold" 
    missionTimers[element].scale = tonumber(scale) or 1 
    missionTimers[element].hurrytime = 15000 
    missionTimers[element].formatWidth = dxGetTextWidth(missionTimers[element].timerFormat, missionTimers[element].scale, missionTimers[element].font) 
    missionTimers[element].colour = (r and g and b) and tocolor(r, g, b) or tocolor(255,255,255) 
    missionTimers[element].timer = setTimer ( triggerEvent, duration, 1, "onClientMissionTimerElapsed", element ) 
end 
  
function setMissionTimerTime ( timer, time ) 
    if missionTimers[timer] then 
        missionTimers[timer].duration = tonumber(time) or missionTimers[timer].remaining 
        missionTimers[timer].originalTick = getTickCount() 
         
        if isTimer( missionTimers[timer].timer ) then 
            killTimer ( missionTimers[timer].timer ) 
        end 
        missionTimers[timer].timer = setTimer ( triggerEvent, missionTimers[timer].duration, 1, "onClientMissionTimerElapsed", element )             
             
        return true 
    end 
    return false 
end 
  
function getMissionTimerTime ( timer ) 
    if missionTimers[timer] then 
        if missionTimers[timer].countdown then 
            return math.max(missionTimers[timer].duration - (getTickCount() - missionTimers[timer].originalTick),0) 
        else 
            return (getTickCount() - missionTimers[timer].originalTick) 
        end 
    end 
    return false 
end 
  
function setMissionTimerFrozen ( timer, frozen ) 
    if frozen == not not missionTimers[timer].frozen then return false end 
    if missionTimers[timer] and bool[frozen] then 
        missionTimers[timer].frozen = frozen or nil 
        if frozen then 
            if isTimer( missionTimers[timer].timer ) then 
                killTimer ( missionTimers[timer].timer ) 
            end 
            missionTimers[timer].timer = nil 
            missionTimers[timer].duration = getMissionTimerTime ( timer ) 
        else 
            missionTimers[timer].timer = setTimer ( triggerEvent, missionTimers[timer].duration, 1, "onClientMissionTimerElapsed", timer )       
            missionTimers[timer].originalTick = getTickCount() 
        end 
        return true 
    end 
    return false 
end 
  
function isMissionTimerFrozen ( timer ) 
    return not not missionTimers[timer].frozen 
end 
  
function setMissionTimerHurryTime ( timer, time ) 
    missionTimers[timer].hurrytime = tonumber(time) or 15000 
end 
  
function setMissionTimerFormat( timer, timerFormat ) 
    if type( timerFormat ) ~= "string" then return false end 
     
    if missionTimers[timer] then 
        missionTimers[timer].timerFormat = timerFormat 
        missionTimers[timer].formatWidth = dxGetTextWidth(missionTimers[timer].timerFormat, missionTimers[timer].scale, missionTimers[timer].font) 
    end 
end 
  
function onMissionTimerDestroy() 
    for i,timer in ipairs(getTimers()) do 
        if timer == missionTimers[source].timer then 
            killTimer ( timer ) 
            break 
        end 
    end 
    missionTimers[source] = nil 
en 

Link to comment
45: Trigger Event format is wrong https://wiki.multitheftauto.com/wiki/TriggerEvent

112: One of the values in there is a nil value, meaning it does not exist, not sure which /I'm tired../

test\timer2.lua:17: Bad argument @ 'destroyElement' [Expected element at argument 1] -- Code not posted, not sure of the issue --

Code here. I already put it in my second post:

addEventHandler("onResourceStart", root, 
        function() 
            test = exports.missiontimer:createMissionTimer (5000,true,"%m:%s",0.5,20,true,"default-bold",1,255,255,255) 
        end 
    ) 
      
      
    addEventHandler("onMissionTimerElapsed", root, 
        function() 
            destroyElement(test) 
        end 
    ) 
      
    addCommandHandler("tdmdt", 
        function () 
            if ( test ) then 
                destroyElement(test) 
            end 
        end 
    ) 

Link to comment
Oh sorry

replace

  if ( test ) then 
                destroyElement(test) 
            end 
        end 

with

  if test then 
                destroyElement(test) 
            end 
        end 

What does that have to do with anything? The parentheses is just a coding convention. It has no effect, at least in Lua, on how the code works. Anyway, I'm still getting the same errors.

[2016-02-14 16:38:37] ERROR: missiontimer\missiontimer_client.lua:112: attempt to index field '?' (a nil value)

[2016-02-14 16:38:42] WARNING: missiontimer\missiontimer_client.lua:45: Bad argument @ 'triggerEvent' [Expected element at argument 2]

[2016-02-14 16:46:40] WARNING: test\timer2.lua:17: Bad argument @ 'destroyElement' [Expected element at argument 1]

Link to comment

Well [2016-02-14 16:38:42] WARNING: missiontimer\missiontimer_client.lua:45: Bad argument @ 'triggerEvent' [Expected element at argument 2] your triggerEvent format is wrong https://wiki.multitheftauto.com/wiki/TriggerEvent fix it.

[2016-02-14 16:46:40] WARNING: test\timer2.lua:17: Bad argument @ 'destroyElement' [Expected element at argument 1] - It's bypassing the if ( test ) then and just going straight to it. Try this

if (test) then 
destroyElement(test) 
else 
end 
end 

As for [2016-02-14 16:38:37] ERROR: missiontimer\missiontimer_client.lua:112: attempt to index field '?' (a nil value) I'm not sure.

Link to comment

Just so you know, I didn't make the missiontimer resource. Talidan did. And also this:

Well [2016-02-14 16:38:42] WARNING: missiontimer\missiontimer_client.lua:45: Bad argument @ 'triggerEvent' [Expected element at argument 2] your triggerEvent format is wrong https://wiki.multitheftauto.com/wiki/TriggerEvent fix it.

[2016-02-14 16:46:40] WARNING: test\timer2.lua:17: Bad argument @ 'destroyElement' [Expected element at argument 1] - It's bypassing the if ( test ) then and just going straight to it. Try this

if (test) then 
destroyElement(test) 
else 
end 
end 

As for [2016-02-14 16:38:37] ERROR: missiontimer\missiontimer_client.lua:112: attempt to index field '?' (a nil value) I'm not sure.

It's going by the syntax of setTimer, theFunction, timeInterval, timesToExecute, and then the arguments (which is the syntax of triggerEvent).
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...