Jump to content

KillTimer problem


Overkillz

Recommended Posts

Hey dear community, Im having a simple issue with killTimer

Well, my problem is that I want to remove the ElementData after 12 seconds if the car doesnt get another hit on this time, however, if he gets the hit, cancell the timer and restart it again.

The script works pretty well, but Im getting a annoying message at DebugScript.

function removeLastHit() 
    setElementData(localPlayer, "ultimateHi", false) 
    outputChatBox("Hit Removed | Time Exceded",255,255,255,true) 
end 
  
addEventHandler("onClientVehicleCollision", root, 
    function ( hit )  
        if ( source == getPedOccupiedVehicle(localPlayer) ) then 
             if getElementType ( hit ) == "vehicle" then 
                if(getVehicleOccupant ( hit, 0 ) ~= false) then 
                    setElementData(localPlayer, "ultimateHi", "Whatever") 
                    killTimer ( killerCleaner ) 
                    killerCleaner = setTimer(removeLastHit,5000,1) 
                end 
            end 
        end 
    end 
) 

- Image: http://puu.sh/ov64i/74e86c54c4.png

I hope you can help me.

Thanks for your time.

Regards.

Link to comment

Thanks so much, Im trying do do a DD Killer System but I cant get the idea on my mind how can I set a system to increase kills.

Im already using the funciton

onPlayerWasted (I wont use onClientPlayerWasted) because Im gonna use some other functions.

function updateKiller() 
    local thePlayer = getPlayerNametagText(source) 
    local killer = getElementData(source,"lastHit")  
    if killer then 
    outputChatBox(thePlayer.." #ffffffhave been killed by: #00ff00"..killer,getRootElement(),255,255,255,true) 
    end 
end 
addEventHandler( "onPlayerWasted", getRootElement(),updateKiller) 

I would apreciate you the idea for it.

Link to comment
-- you must change this 
setElementData(localPlayer, "ultimateHi", "Whatever") 
-- to 
local driver = getVehicleController(hit) 
setElementData(localPlayer, "ultimateHi",  driver) 
  
function updateKiller() 
    local thePlayer = getPlayerNametagText(source) 
    local killer = getElementData(source,"lastHit") 
    if killer then 
    local killerName = getPlayerNametagText(killer) 
    outputChatBox(thePlayer.." #ffffffhave been killed by: #00ff00"..killerName, getRootElement(),255,255,255,true) 
    local previousKills = getElementData(killer, "kills") or 0 
    setElementData(killer, "kills", previousKills + 1) 
    end 
end 
addEventHandler( "onPlayerWasted", getRootElement(),updateKiller) 

Link to comment

Thanks, both methods works fine.

However, now Im looking for add the Killer with the number of kills that he has done.

I tried to insert them into a table, but unlucky, I did something wrong.

  
killerTable = {} 
if ( not killerTable [ killer ] ) then 
        killerTable [ killer ] = { } 
    end 
    table.insert(killerTable [ killer ],1) 

I hope u can give me an easy way, I want to build a ranking table (dX) which shows the top killers

Link to comment

Ehm, i think that better than your code, i am not sure but it should work. (wrote on my phone)

killerTable = {} 
    if ( not killerTable [ killer ] ) then 
        -- if it is first kill, set player table value '1' 
        killerTable [ killer ] = 1 
    end 
    -- if it isnt first kill, set player table value 'previous kills' + '1' 
    killerTable [ killer ] = killerTable [ killer ] + 1 

Link to comment

Thanks mate, this is the alst thing that I will request you, how can I get the name of the Player from a table, I tried it but It dropped me an error. Also, to sort them dempending the number of kills it should be table.sort(killerTable [ killer ]) right.

Thanks for your time. :)

Link to comment
  
local playerName = getPlayerName( killerTable[ player ] ) 
  

It gives number of kill in my code...

Here is your code, Overkillz

killerTable = {} 
if ( not killerTable[killer] ) then 
    killerTable[killer] = {playerName = getPlayerNametagText(killer), playerKills = 1} 
end 
killerTable[killer].playerKills = killerTable[killer].playerKills + 1 

Table sorting, getting player name and kills with loop

table.sort (killerTable, function (a, b) return a.playerKills > b.playerKills end) 
  
for _, value in pairs(killerTable) do 
   local playerName = value.playerName 
   local playerKills = value.playerKills 
   outputChatBox("playerName: "..playerName..", playerKills: "..playerKills) 
end 

Link to comment

Thanks it worked pretty fine, however, there is a problem sorting the table.

When are output, it drops the order depending of the first player that kill, later the 2nd killer and not following the order of kills.

I tought it was a simple problem adding tonumber but it didnt work

table.sort(killerTable,function(a,b) return (tonumber( a.playerKills )) < (tonumber( b.playerKills )) end) 

Also, there is a way to trigger a table ?

Link to comment
Thanks it worked pretty fine, however, there is a problem sorting the table.

When are output, it drops the order depending of the first player that kill, later the 2nd killer and not following the order of kills.

That's because the sorting wasn't completed when you are looping. So in server-side, sort table. Then send the table to client-side, and loop it.

--server side 
table.sort (killerTable, function (a, b) return a.playerKills > b.playerKills end) 
-- define the 'theElement' 
triggerClientEvent(theElement, "sendClientToTable", resourceRoot, killerTable) 
  
--client side 
function blabla(tableFromServerside) 
    killerTable = tableFromServerside 
end 
addEvent("sendClientToTable", true) 
addEventHandler("sendClientToTable", root, blabla) 
  
function loopIt() 
if killerTable then 
   for _, value in pairs(killerTable) do 
      local playerName = value.playerName 
      local playerKills = value.playerKills 
      outputChatBox("playerName: "..playerName..", playerKills: "..playerKills) 
   end 
end 
end 
addCommandHandler("loop", loopIt) 

Link to comment

Thanks, I got it, but when Im trying to draw the table, it doesnt work.

Its something defineing wrongly ?

for id=1,#killerTable do 
    dxDrawText(killerTable[id]['killer'].. " Kills: "..killerTable[id]['killer']..playerKills,sX/2,sY/2,0,0,tocolor(255,255,255,255),1,"default-bold","left","top",false,false,false,true) 
    end 

Thanks and regards.

Link to comment

There is no indexed value in killerTable so you can't use indexed loop (ipairs and your loop). So use pairs loop.

for _, value in pairs(killerTable) do 
    dxDrawText(value.playerName.. " Kills: "..value.playerKills,sX/2,sY/2,0,0,tocolor(255,255,255,255),1,"default-bold","left","top",false,false,false,true) 
end 

Link to comment

Well, thats nice, if I want to add diferent colours or whatever I want for the first place, should I do this ?

for _, value in pairs(killerTable) do 
        for i=1,1 do 
        dxDrawText(value.playerName.. " Kills: "..value.playerKills,sX/2,sY/2,0,0,tocolor(255,255,255,255),1,"default-bold","left","top",false,false,false,true) 
        end 
        for i=2,8 do 
        dxDrawText(value.playerName.. " Kills: "..value.playerKills,sX/2,sY/2,0,0,tocolor(255,255,255,255),1,"default-bold","left","top",false,false,false,true) 
        end 
    end 

Link to comment

Don't know if it works but if i were you, i would use variable to get player rank like that.

local rank = 1 
for _, value in pairs(killerTable) do 
        local color = "#FFFFFF" 
        if rank == 1 then color = "#FF0000" 
        elseif rank == 2 then color = "#FFFF00" 
        elseif rank == 3 then color = "#00FF00" 
        end 
        local playerNameWithoutHex = removeColorCoding(value.playerName) -- removing hex color codes from nick... 
        dxDrawText(color..playerNameWithoutHex.. " Kills: "..value.playerKills,sX/2,sY/2,0,0,tocolor(255,255,255,255),1,"default-bold","left","top",false,false,false,true) 
        rank = rank + 1 --updating rank for next player... 
    end 
  
-- remove color coding from string 
function removeColorCoding ( name ) 
    return type(name)=='string' and string.gsub ( name, '#%x%x%x%x%x%x', '' ) or name 
end 

Link to comment

Thanks, however I did my own way

for id, value in pairs(killerTable) do 
        if id == 1 then 
            dxDraw.... 
        end 
        if id >= 2 then 
            dxDraw.... 
        end 
    end 

Thanks for helping.

There is a way to check if a table is empty or to delete the table fully ?

Link to comment

Thanks a lot of dude, however, I have been doing my test triggering the table with the values to use it with my Lib

  
--Server Trigger to client 
function addInto() 
    for i=1,5 do 
    if ( not killerTable["Name"..i] ) then 
        killerTable["Name"..i] = {playerName = "Name "..i, playerKills = 0} 
    end 
    killerTable["Name"..i].playerKills = killerTable["Name"..i].playerKills + (math.random(1,8)) 
    triggerClientEvent(getRootElement(), "sendClientToTable", resourceRoot, killerTable) 
    outputChatBox("Added Correctly") 
    end 
end 
addCommandHandler("add",addInto) 
  
-- Trigger Client Side 
function blabla(tableFromServerside) 
    --killerTable = {} 
    killerTable = tableFromServerside 
    if killerTable then 
        table.sort (killerTable, function (a, b) return a.playerKills > b.playerKills end) 
    end 
    outputChatBox("Table Triggered Correctly") 
end 
addEvent("sendClientToTable", true) 
addEventHandler("sendClientToTable", root, blabla) 
  
--Client Draw with my Lib 
for id, value in pairs(killerTable) do   
        if tonumber(id) == tonumber(1) then 
            dxLibTopwinners(ddKill.posT,(sY*0.038+34*scale),sizeXT,sizeYT,killerTable[id].playerName,1*font_size,fontTopKillers,255,255,255,ddKill.alphaT[1],0,0,0,ddKill.alphaT[2],sizeYT*0.8,sizeYT*0.8,255,136,0,ddKill.alphaT[1],"Kills: "..killerTable[id].playerKills or "?","Hits: ".."",false,false) 
        end 
        if tonumber(id) == tonumber(2) then 
            dxLibTopwinners(ddKill.posT,(sY*0.038+34*scale)+sizeYT*1.05,sizeXT,sizeYT,killerTable[id].playerName,1*font_size,fontTopKillers,255,255,255,ddKill.alphaT[1],0,0,0,ddKill.alphaT[2],sizeYT*0.8,sizeYT*0.8,105,105,105,ddKill.alphaT[1],"Kills: "..killerTable[id].playerKills or "?","Hits: ".."",false,false) 
        end 
        if tonumber(id) == tonumber(3) then 
            dxLibTopwinners(ddKill.posT,(sY*0.038+34*scale)+sizeYT*1.05*2,sizeXT,sizeYT,killerTable[id].playerName,1*font_size,fontTopKillers,255,255,255,ddKill.alphaT[1],0,0,0,ddKill.alphaT[2],sizeYT*0.8,sizeYT*0.8,139,54,38,ddKill.alphaT[1],"Kills: "..killerTable[id].playerKills or "?","Hits: ".."",false,false) 
        end 
--The next line is the 146 
        if tonumber(id) >= tonumber(4) and tonumber(id) <= tonumber(8) then 
            dxLibToptime(ddKill.posT,(sY*0.038+34*scale)+sizeYT*1.05*(id-1),sizeXT,sizeYT,killerTable[id].playerName,1*font_size,fontTopKillers,255,255,255,ddKill.alphaT[1],0,0,0,ddKill.alphaT[2],sizeYT*0.8,sizeYT*0.8,45,60,81,ddKill.alphaT[1],id,"Kills: "..killerTable[id].playerKills or "?","Hits: ".."",1.2*font_size,false,false) 
        end 
    end 
  

-- Variables are defined, but I got this error after triggering on the line 146 (I marked it on the script)

- http://puu.sh/owTvk/5c5bec27ef.png (Image)

I hope that u can help me to solve this problem, I have been more than 2 days working on this.

Thanks

Link to comment

Seems you didn't understand getting value from table in loop or my language skills not enough to explain it better :)

killerTable[id].playerName must be value.playerName

killerTable[id].playerKills must be value.playerKills

--Client Draw with my Lib 
for id, value in pairs(killerTable) do  
        if tonumber(id) == tonumber(1) then 
            dxLibTopwinners(ddKill.posT,(sY*0.038+34*scale),sizeXT,sizeYT,value.playerName,1*font_size,fontTopKillers,255,255,255,ddKill.alphaT[1],0,0,0,ddKill.alphaT[2],sizeYT*0.8,sizeYT*0.8,255,136,0,ddKill.alphaT[1],"Kills: "..value.playerKills or "?","Hits: ".."",false,false) 
        end 
        if tonumber(id) == tonumber(2) then 
            dxLibTopwinners(ddKill.posT,(sY*0.038+34*scale)+sizeYT*1.05,sizeXT,sizeYT,value.playerName,1*font_size,fontTopKillers,255,255,255,ddKill.alphaT[1],0,0,0,ddKill.alphaT[2],sizeYT*0.8,sizeYT*0.8,105,105,105,ddKill.alphaT[1],"Kills: "..value.playerKills or "?","Hits: ".."",false,false) 
        end 
        if tonumber(id) == tonumber(3) then 
            dxLibTopwinners(ddKill.posT,(sY*0.038+34*scale)+sizeYT*1.05*2,sizeXT,sizeYT,value.playerName,1*font_size,fontTopKillers,255,255,255,ddKill.alphaT[1],0,0,0,ddKill.alphaT[2],sizeYT*0.8,sizeYT*0.8,139,54,38,ddKill.alphaT[1],"Kills: "..value.playerKills or "?","Hits: ".."",false,false) 
        end 
--The next line is the 146 
        if tonumber(id) >= tonumber(4) and tonumber(id) <= tonumber(8) then 
            dxLibToptime(ddKill.posT,(sY*0.038+34*scale)+sizeYT*1.05*(id-1),sizeXT,sizeYT,value.playerName,1*font_size,fontTopKillers,255,255,255,ddKill.alphaT[1],0,0,0,ddKill.alphaT[2],sizeYT*0.8,sizeYT*0.8,45,60,81,ddKill.alphaT[1],id,"Kills: "..value.playerKills or "?","Hits: ".."",1.2*font_size,false,false) 
        end 
    end 

Link to comment
It is the samething.

id is the index and value is the value of that index.

So killerTable[id].playerName = value.playerName

If you don't know tables, please don't speak... Have you ever tried killerTable[id].playerName ? I have tried, it returns nil.

Link to comment
It is the samething.

id is the index and value is the value of that index.

So killerTable[id].playerName = value.playerName

If you don't know tables, please don't speak... Have you ever tried killerTable[id].playerName ? I have tried, it returns nil.

You bet?

Try this.

  
t1 = { 
['hi']="yo", 
[2]="this" 
} 
  
for i, v in ipairs( t1 ) do 
      outputChatBox( "Anubhav: "..t1[i].." Ozulus:"..v ) 
end 
  

EDIT: I found the problem. The error shows your comparing is wrong, ozulus, you were wrong here.

The problem I think is that id is not a number causing nil value.

  
  
  
--Server Trigger to client 
function addInto() 
    for i=1,5 do 
    if ( not killerTable["Name"..i] ) then 
        killerTable["Name"..i] = {playerName = "Name "..i, playerKills = 0} 
    end 
    killerTable["Name"..i].playerKills = killerTable["Name"..i].playerKills + (math.random(1,8)) 
    triggerClientEvent(getRootElement(), "sendClientToTable", resourceRoot, killerTable) 
    outputChatBox("Added Correctly") 
    end 
end 
addCommandHandler("add",addInto) 
  
-- Trigger Client Side 
function blabla(tableFromServerside) 
    --killerTable = {} 
    killerTable = tableFromServerside 
    if killerTable then 
        table.sort (killerTable, function (a, b) return a.playerKills > b.playerKills end) 
    end 
    outputChatBox("Table Triggered Correctly") 
end 
addEvent("sendClientToTable", true) 
addEventHandler("sendClientToTable", root, blabla) 
  
--Client Draw with my Lib 
for id, value in pairs(killerTable) do  
        local number = id:gsub( "Name", "" ) 
        if tonumber(number) == tonumber(1) then 
            dxLibTopwinners(ddKill.posT,(sY*0.038+34*scale),sizeXT,sizeYT,killerTable[id].playerName,1*font_size,fontTopKillers,255,255,255,ddKill.alphaT[1],0,0,0,ddKill.alphaT[2],sizeYT*0.8,sizeYT*0.8,255,136,0,ddKill.alphaT[1],"Kills: "..killerTable[id].playerKills or "?","Hits: ".."",false,false) 
        end 
        if tonumber(number) == tonumber(2) then 
            dxLibTopwinners(ddKill.posT,(sY*0.038+34*scale)+sizeYT*1.05,sizeXT,sizeYT,killerTable[id].playerName,1*font_size,fontTopKillers,255,255,255,ddKill.alphaT[1],0,0,0,ddKill.alphaT[2],sizeYT*0.8,sizeYT*0.8,105,105,105,ddKill.alphaT[1],"Kills: "..killerTable[id].playerKills or "?","Hits: ".."",false,false) 
        end 
        if tonumber(number) == tonumber(3) then 
            dxLibTopwinners(ddKill.posT,(sY*0.038+34*scale)+sizeYT*1.05*2,sizeXT,sizeYT,killerTable[id].playerName,1*font_size,fontTopKillers,255,255,255,ddKill.alphaT[1],0,0,0,ddKill.alphaT[2],sizeYT*0.8,sizeYT*0.8,139,54,38,ddKill.alphaT[1],"Kills: "..killerTable[id].playerKills or "?","Hits: ".."",false,false) 
        end 
--The next line is the 146 
        if tonumber(number) >= tonumber(4) and tonumber(number) <= tonumber(8) then 
            dxLibToptime(ddKill.posT,(sY*0.038+34*scale)+sizeYT*1.05*(id-1),sizeXT,sizeYT,killerTable[id].playerName,1*font_size,fontTopKillers,255,255,255,ddKill.alphaT[1],0,0,0,ddKill.alphaT[2],sizeYT*0.8,sizeYT*0.8,45,60,81,ddKill.alphaT[1],id,"Kills: "..killerTable[id].playerKills or "?","Hits: ".."",1.2*font_size,false,false) 
        end 
    end 
  

This should work!

Link to comment

You bet?

Try this.

  
t1 = { 
['hi']="yo", 
[2]="this" 
} 
  
for i, v in ipairs( t1 ) do 
      outputChatBox( "Anubhav: "..t1[i].." Ozulus:"..v ) 
end 
  

t1[1] returns nil. Only t1[2] gives the value which is "this". You are still wrong... Your solution is good btw. but its value.playerName , value.playerKills not killerTable[id].playerName , killerTable[id].playerKills

Link to comment

EDIT: My bad.

It should be pairs.

You probably don't know how tables works.

pairs still gives you the index.

if the index ['player'] it will show that.

and if you want to check it's value you can use table.something or table['player'].something or v.something. They're the samething. Just different methods.

Link to comment

if the index ['player'] it will show that.

and if you want to check it's value you can use table.something or table['player'].something or v.something. They're the samething. Just different methods.

I'm trying to explain that already. The index in this loop is a NUMBER. Not a player element. Peace :)

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