Jump to content

Help with killTimer


beatles1

Recommended Posts

What I want to do it to set a timer using a players name and the word timer when a player enters a col and then kill the timer when they leave.

Server:

function aFunction(sourcePlayer) 
[getPlayerName ( sourcePlayer ).."Timer"] = setTimer ( anotherFunction, 120000, 1 ) 
end 
  
function anotherAnotherFunction ( sourcePlayer ) 
killTimer(getPlayerName ( sourcePlayer ).."Timer") 

I get an error about not being able to give the timer a non boolean value. How would I do this then? I am assigning names as all the timers need to be associated with players.

Link to comment
local timers = {} 
  
function aFunction(sourcePlayer) 
    timers[getPlayerName ( sourcePlayer ).."Timer"] = setTimer ( anotherFunction, 120000, 1, sourcePlayer ) 
end 
  
function anotherAnotherFunction ( sourcePlayer ) 
    if isTimer(timers[getPlayerName ( sourcePlayer ).."Timer"]) then 
        killTimer(timers[getPlayerName ( sourcePlayer ).."Timer"]) 
    end 
end 

Link to comment

It is not humane.

  
local timers = { } 
  
function aFunction( sourcePlayer ) 
    timers[ sourcePlayer ] = setTimer ( anotherFunction, 120000, 1, sourcePlayer ) -- call anotherFunction  
end 
  
function anotherFunction ( sourcePlayer ) 
    if isTimer( timers[ sourcePlayer ] ) then 
        killTimer( timers[ sourcePlayer ] ) 
        timers[ sourcePlayer ] = nil 
    end 
end 
  
addEventHandler( 'onPlayerQuit',root, 
    function( ) 
        timers[ source ] = nil 
    end 
) 
  

Index in table is player element( userdata ).

Link to comment

Thanks guys that works great.

EDIT: Ignore the below, stupid mistake :oops:

As its kinda a similar question, how would I do the same for an object. I want a player to be able to spawn (preferebly about 5) of the traffic cone object and then delete them via commands. I started with 1 cone at a time and used the same as with the timers:

  
local cone = { } 
function spawnCones (sourcePlayer) 
 local x,y,z = getElementPosition ( sourcePlayer ) 
 cone[sourcePlayer] = createObject ("1238", x, y, z-0.6) 
end 
addCommandHandler ( "cone", spawnCones) 
  
function destroyCones() 
 destroyElement(cone[sourcePlayer]) 
end 
addCommandHandler ( "deletecone", destroyCones) 
  

Having no luck so far, sorry for me not properly understanding LUA properly, I should probably buy a book or something.

Link to comment
local cones = {} 
  
function spawnCones (thePlayer) 
    if (not cones[thePlayer]) then cones[thePlayer] = {} end 
    local x,y,z = getElementPosition ( thePlayer ) 
    local index = #cones[thePlayer] +1 
    cones[thePlayer][index] = createObject (1238, x, y, z-0.6) 
    outputChatBox("Created cone, ID: ".. index .."!",thePlayer,0,255,0) 
end 
addCommandHandler ( "cone", spawnCones) 
  
function destroyCones(thePlayer, cmd, id) 
    if (not cones[thePlayer]) then cones[thePlayer] = {} end 
    local id = tonumber(id) 
    if isElement(cones[thePlayer][id]) then  
        destroyElement(cones[thePlayer][id]) 
        outputChatBox("Removed cone, ID: ".. id .."!",thePlayer,0,255,0) 
    end 
end 
addCommandHandler ( "deletecone", destroyCones) 

Link to comment
local cones = {} 
  
function spawnCones (thePlayer) 
    if (not cones[thePlayer]) then cones[thePlayer] = {} end 
    local x,y,z = getElementPosition ( thePlayer ) 
    local index = #cones[thePlayer] +1 
    cones[thePlayer][index] = createObject (1238, x, y, z-0.6) 
    outputChatBox("Created cone, ID: ".. index .."!",thePlayer,0,255,0) 
end 
addCommandHandler ( "cone", spawnCones) 
  
function destroyCones(thePlayer, cmd, id) 
    if (not cones[thePlayer]) then cones[thePlayer] = {} end 
    local id = tonumber(id) 
    if isElement(cones[thePlayer][id]) then  
        destroyElement(cones[thePlayer][id]) 
        outputChatBox("Removed cone, ID: ".. id .."!",thePlayer,0,255,0) 
    end 
end 
addCommandHandler ( "deletecone", destroyCones) 

Wow thats great, much better than what I had scripted :D . But it takes a long time to remove a lot of cones 1 by one and I need to be able to remove all of a players cones when they disconnect. How would I remove all cones, do I need to go through all numbers to 100? And if so it might be a good idea to limit the amount of cones that can be spawned.

Link to comment
local cones = { } 
  
function spawnCones ( thePlayer ) 
    if not cones[ thePlayer ] then cones[ thePlayer ] = { } end 
    local x,y,z = getElementPosition ( thePlayer ) 
    local index = #cones[ thePlayer ] +1 
    cones[ thePlayer ][ index ] = createObject ( 1238, x, y, z-0.6 ) 
    outputChatBox( "Created cone, ID: ".. index .."!",thePlayer,0,255,0 ) 
end 
addCommandHandler ( "cone", spawnCones ) 
  
function destroyCones( thePlayer, cmd, id ) 
    if not cones[ thePlayer ] then cones[ thePlayer ] = { } end 
    local id = tonumber( id ) 
    if isElement( cones[ thePlayer ][ id ] ) then  
        destroyElement( cones[ thePlayer ][ id ] ) 
        outputChatBox( "Removed cone, ID: ".. id .."!",thePlayer,0,255,0 ) 
    end 
end 
addCommandHandler ( "deletecone", destroyCones ) 
  
addEventHandler( 'onPlayerQuit',root, -- if player quit then remove all cones. 
    function( ) 
        if #cones[ source ] > 0 then 
            for i = 1,#cones[ source ] do 
                destroyElement( cones[ source ][ i ] ) 
                cones[ source ] = nil 
            end 
        end  
    end 
)    

Edited by Guest
Link to comment
local cones = {} 
  
function spawnCones (thePlayer) 
    if (not cones[thePlayer]) then cones[thePlayer] = {} end 
    local x,y,z = getElementPosition ( thePlayer ) 
    local index = #cones[thePlayer] +1 
    cones[thePlayer][index] = createObject (1238, x, y, z-0.6) 
    outputChatBox("Created cone, ID: ".. index .."!",thePlayer,0,255,0) 
end 
addCommandHandler ( "cone", spawnCones) 
  
function destroyCones(thePlayer) 
    if (not cones[thePlayer]) then cones[thePlayer] = {} end 
    for index, cone in pairs(cones[thePlayer]) do 
        if isElement(cone) then 
            destroyElement(cone) 
        end 
    end 
    outputChatBox("Removed cones!",thePlayer,0,255,0) 
end 
addCommandHandler ( "deletecone", destroyCones) 

That'll remove all player cones.

Link to comment

Thanks that's really great. Just one last question though, I cant get getElementType to work.

My code is like so:

function aFunction(sourcePlayer) 
if getElementType ( sourcePlayer ) == "ped" then 
Do something 
end 
end 
  
addEventHandler ( "onColShapeLeave", aCol, aFunction ) 

But nothing is get recognised as a "ped" whereas if I comment out the end and the if statement it runs fine (I need it to stop warnings when a non ped goes through the col). Am I doing something wrong in this snippet or should I be taking a wider look at my code to find the problem?

Link to comment

It should only run for players but I tried "player" as well. What I need to do is stop it from running for vehicles. I thought players counted as peds though.

And am I right in thinking that when you enter a col in a vehicle both the vehicle and the player hit the col separately as the player is still another element.

Link to comment
function aFunction( element,dim ) 
    if getElementType ( element ) == "player" and not isPedInVehicle( element ) then -- check if player hit colshape and player not in vehicle. 
        --Do something 
    end 
end 
addEventHandler ( "onColShapeLeave", aCol, aFunction ) 

You mean this?

Link to comment

No, I only want it to run for players if in a car or not. The problem is that otherwise it is checking loads of player only things on the cars that get spawned within the col and cars that the player drives into the col that give warnings. I think I was right at first and must have just made a small mistake somewhere.

So this:

  
function aFunction( element,dim ) 
    if getElementType ( element ) == "player" then -- check if player hit colshape 
        --Do something 
    end 
end 
addEventHandler ( "onColShapeLeave", aCol, aFunction ) 
  

Will only run the code if the element hitting the col is a player and it will work if he is in a car as well?

Thanks for the help guys, I really appreciate it.

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