Jump to content

Issue with setTimer and onVehicleWeaponFire


Bilal135

Recommended Posts

I wanted to shoot missiles with intervals of 5 seconds between them. Tried the following code, but it only works for the first or second time when shooting missiles, then it shoots like normal (no interval). Tried different ways but couldn't fix it. Any help regarding this?

armedVehicles = {[425]=true, [520]=true, [476]=true, [447]=true, [430]=true, [432]=true, [464]=true, [407]=true, [601]=true}
function vehicleWeaponFire(thePresser, key, keyState, vehicleFireType)
    local vehModel = getElementModel(getPedOccupiedVehicle(thePresser))
    if (armedVehicles[vehModel]) then
        triggerEvent("onVehicleWeaponFire", thePresser, vehicleFireType, vehModel)
    end
end

function bindOnJoin()
    bindKey(source, "vehicle_fire", "down", vehicleWeaponFire, "primary")
    bindKey(source, "vehicle_secondary_fire", "down", vehicleWeaponFire, "secondary")
end
addEventHandler("onPlayerJoin", root, bindOnJoin)

function bindOnStart()
    for index, thePlayer in pairs(getElementsByType("player")) do
        bindKey(thePlayer, "vehicle_fire", "down", vehicleWeaponFire, "primary")
        bindKey(thePlayer, "vehicle_secondary_fire", "down", vehicleWeaponFire, "secondary")
    end
end
addEventHandler("onResourceStart", getResourceRootElement(), bindOnStart)

function stopMissileSpam()
player = source
toggleControl(player, "vehicle_secondary_fire", false)
toggleControl(player, "vehicle_fire", false)
setTimer(function()
toggleControl(player, "vehicle_secondary_fire", true) 
toggleControl(player, "vehicle_fire", true)
end, 5000, 1)
end
addEvent("onVehicleWeaponFire", true)
addEventHandler("onVehicleWeaponFire", root, stopMissileSpam)

 

Link to comment
  • Moderators

I am not sure how those rockets are created, but why not solve the issue at the source?

local nextShootTime = 0

function vehicleWeaponFire(thePresser, key, keyState, vehicleFireType)
	local vehModel = getElementModel(getPedOccupiedVehicle(thePresser))
  
  	local timeNow = getTickCount() -- system time
	if (armedVehicles[vehModel]) and timeNow > nextShootTime  then -- if system time is higher than nextShootTime
		nextShootTime = timeNow + 5000 -- system time + 5000 ms = nextShootTime
 
		triggerEvent("onVehicleWeaponFire", thePresser, vehicleFireType, vehModel)
	end
end

 

 

And regarding your last issue:

I think it is this:

player = source

 

It is a global after all, other players can also change the player variable globally before the timer is finished.

 

So:

local player = source

But even so, I am not sure if the toggleControl function can block the bindKey. I put my doubts on that.

 

 

Edited by IIYAMA
Link to comment
Guest
This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...