Jump to content

Stop nitro from refilling itself automatically and refill with a timer instead


koragg

Recommended Posts

I got this client-side script which adds 3 modes for the nitro system:

-Old = Default MTA/GTA nitro, press to use, wait until it's empty.

-Hybrid = Press key to use, press same key again to stop using nitro.

-NFS = Hold key to use nitro, when you release the key, nitro stops.

 

Now my problem's that in all those modes the nitro starts recharging itself automatically as soon as I stop using it. How to prevent that? I mean this:

I'm using NFS mode, I hold the key to use nos and the moment I let go of the key my nitro starts recharging itself until it reaches 100% again. I want to disable this recharging so that if I m at 40% nos when I let go of the key, it would stay at 40% until I get another nos pickup. And after let's say 1 min the nos will go to 100% again ONLY IF it was empty before that.

 

I know that there's a resource called 'race_nos2' and I want my script to do exactly the same like it, just without removing the nitro upgrade as that way the nitro meter will always show the correct amount left, unlike if the upgrade is removed/added each time (like the race_nos2 way). Really need help with this...

 

local sx,sy = guiGetScreenSize()
local px,py = 1600,900
local x,y =  (sx/px), (sy/py)
-------------------------------------------------------------------------------------------------------------------------
-------------------
---   NFS NOS   ---
-------------------
local nostype = 'nfs'
function toggleNitro(k, s)
	local v = getPedOccupiedVehicle(localPlayer)
	if v and getVehicleUpgradeOnSlot(v, 8) ~= 0 and not isElementFrozen(v) then
		if nostype == 'hybrid' then
			if s=='down' then
				setVehicleNitroActivated(v, not isVehicleNitroActivated(v))
				setElementData(v, 'nitro', {s==not isVehicleNitroActivated(v), getVehicleNitroLevel(v)})
			end
		elseif nostype == 'nfs' then
			setVehicleNitroActivated(v, s=='down')
			setElementData(v, 'nitro', {s==not isVehicleNitroActivated(v), getVehicleNitroLevel(v)})
		end
	end
end
bindKey("vehicle_fire", 'both', toggleNitro)
bindKey("vehicle_secondary_fire", 'both', toggleNitro)
-------------------------------------------------------------------------------------------------------------------------
addEvent'setNitroType'
addEventHandler('setNitroType', root, function(type)
	nostype = type
end)
addCommandHandler('nostype', function(c, type)
	nostype = type
end)
-------------------------------------------------------------------------------------------------------------------------
-- This syncs nitro states and levels from other clients
addEventHandler('onClientElementDataChange', root, function(name)
	if name ~= 'nitro' or getVehicleOccupant(source) == localPlayer then return end
	local data = getElementData(source, 'nitro')
	setVehicleNitroActivated(source, data[1])
	setVehicleNitroLevel(source, data[2])
end)
-------------------------------------------------------------------------------------------------------------------------
-- Nitro Meter
function nitroShow()
	if isPedInVehicle(localPlayer) then
		local car = getCameraTarget()
		local nitro = getVehicleNitroLevel(getPedOccupiedVehicle(localPlayer))
		if car and getElementType(car) == "vehicle" then
			nitro = getVehicleNitroLevel(car)
		end
		if isPlayerMapVisible() then
			return
		end
		if nitro ~= false and nitro ~= nil and nitro > 0 then
			dxDrawText((math.floor(nitro/1*100)).."% Nitro", x*1463+1, y*134.5+1, x*1455+1, y*74+1, tocolor(0, 0, 0, 255), 1.30*y, "default-bold", "center", "center", false, false, false, false, false)
			
			dxDrawRectangle(x*1396, y*92, x*126, y*23, tocolor(44, 44, 44, 255), false)
			dxDrawRectangle(x*1396, y*92, x*126/10*10*nitro, y*23, tocolor(0, 70, 149, 255), false)
			dxDrawText((math.floor(nitro/1*100)).."% Nitro", x*1463, y*134.5, x*1455, y*74, tocolor(255, 255, 255, 255), 1.30*y, "default-bold", "center", "center", false, false, false, false, false)
		else
			dxDrawText("No Nitro", x*1463+1, y*134.5+1, x*1455+1, y*74+1, tocolor(0, 0, 0, 255), 1.30*y, "default-bold", "center", "center", false, false, false, false, false)
			
			dxDrawRectangle(x*1396, y*92, x*126, y*23, tocolor(44, 44, 44, 255), false)
			dxDrawRectangle(x*1396, y*92, x*126, y*23, tocolor(0, 70, 149, 255), false)
			dxDrawText("No Nitro", x*1463, y*134.5, x*1455, y*74, tocolor(255, 255, 255, 255), 1.30*y, "default-bold", "center", "center", false, false, false, false, false)
		end
	end
end
addEventHandler("onClientRender",getRootElement(),nitroShow)
--Edited by MegasXLR

 

Edited by koragg
Link to comment

I'm from phone so sorry for not using code tag. It's a bit bugged from phone. 

?

 
  1. local sx,sy = guiGetScreenSize()
  2. local px,py = 1600,900
  3. local x,y = (sx/px), (sy/py)
  4. -------------------------------------------------------------------------------------------------------------------------
  5. -------------------
  6. --- NFS NOS ---
  7. -------------------
  8. local nostype = 'nfs'
  9. function toggleNitro(k, s)
  10. local v = getPedOccupiedVehicle(localPlayer)
  11. if v and getVehicleUpgradeOnSlot(v, 8) ~= 0 and not isElementFrozen(v) then
  12. if nostype == 'hybrid' then
  13. removeEventHandler ("onClientRender",root,nosFix)
  14. if s=='down' then
  15. setVehicleNitroActivated(v, not isVehicleNitroActivated(v))
  16. setElementData(v, 'nitro', {s==not isVehicleNitroActivated(v), getVehicleNitroLevel(v)})
  17. addEventHandler("onClientRender",root,nosFix)
  18. end
  19. elseif nostype == 'nfs' then
  20. removeEventHandler("onClientRender",root,nosFix)
  21. setVehicleNitroActivated(v, s=='down')
  22. setElementData(v, 'nitro', {s==not isVehicleNitroActivated(v), getVehicleNitroLevel(v)})
  23. addEventHandler("onClientRender",root,nosFix)
  24. end
  25. end
  26. end
  27. bindKey("vehicle_fire", 'both', toggleNitro)
  28. bindKey("vehicle_secondary_fire", 'both', toggleNitro)
  29. ----------------------------
  30. function nosFix ()
  31. local veh = getPedOccupiedVehicle (localPlayer)
  32. local nos = getElementData(localPlayer,"nitro")
  33. If veh and nos~= nil then
  34. setVehicleNitroLevel (veh,nos[2])
  35. end
  36. end

 

Something like this? I'm removing the event handler when key is pressed because i think that onClientRender will set the nitro level to the last known element data while I'm using it, again making it infinite. 

Edited by koragg
Ignore typos if any. Was a nightmare writing code from phone lol
Link to comment

Fixed, thanks!

client:

local sx,sy = guiGetScreenSize()
local px,py = 1600,900
local x,y =  (sx/px), (sy/py)
-------------------------------------------------------------------------------------------------------------------------
-------------------
---   NFS NOS   ---
-------------------
local nostype = 'nfs'
function toggleNitro(k, s)
	local v = getPedOccupiedVehicle(localPlayer)
	if v and getVehicleUpgradeOnSlot(v, 8) ~= 0 and not isElementFrozen(v) then
		if nostype == 'hybrid' then
			if s=='down' then
				setVehicleNitroActivated(v, not isVehicleNitroActivated(v))
				setElementData(v, 'nitro', {s==not isVehicleNitroActivated(v), getVehicleNitroLevel(v)})
			end
		elseif nostype == 'nfs' then
			setVehicleNitroActivated(v, s=='down')
			setElementData(v, 'nitro', {s==not isVehicleNitroActivated(v), getVehicleNitroLevel(v)})
		end
	end
end
bindKey("vehicle_fire", 'both', toggleNitro)
bindKey("vehicle_secondary_fire", 'both', toggleNitro)
-------------------------------------------------------------------------------------------------------------------------
function callNosFix(k, s)
	if nostype == 'nfs' then
		if isEventHandlerAdded("onClientRender", root, nosFix) == false then 
			addEventHandler("onClientRender", root, nosFix)
		end
	elseif nostype == 'hybrid' then
		local veh = getPedOccupiedVehicle(localPlayer)
		local noslvl = false
		if veh and getVehicleUpgradeOnSlot(veh, 8) ~= 0 and not isElementFrozen(veh) then
			local nosOn
			if isVehicleNitroActivated(veh) then
				nosOn = true
			else
				nosOn = false
			end
			noslvl = getVehicleNitroLevel(veh)
			setElementData(veh, 'nitro', {s==not isVehicleNitroActivated(veh), noslvl})
			if isEventHandlerAdded("onClientRender", root, nosFix) == false and nosOn == false then 
				addEventHandler("onClientRender", root, nosFix)
			end
		end
	end
end
bindKey("vehicle_fire", 'up', callNosFix)
bindKey("vehicle_secondary_fire", 'up', callNosFix)
-------------------------------------------------------------------------------------------------------------------------
function stopCallingNosFix()
	if nostype == 'nfs' or nostype == 'hybrid' then
		if isEventHandlerAdded("onClientRender", root, nosFix) == true then
			removeEventHandler("onClientRender", root, nosFix)
		end
	end
end
bindKey("vehicle_fire", 'down', stopCallingNosFix)
bindKey("vehicle_secondary_fire", 'down', stopCallingNosFix)
-------------------------------------------------------------------------------------------------------------------------
function delNos()
	local veh = getPedOccupiedVehicle(localPlayer)
	local nitrolvl = false
	if veh and getVehicleUpgradeOnSlot(veh, 8) ~= 0 and not isElementFrozen(veh) then
		nitrolvl = getVehicleNitroLevel(veh)
	end
	if nitrolvl and math.floor(nitrolvl/1*100) < 1 then
		removeVehicleUpgrade(veh, 1010)
		setTimer(addNos, 40000, 1)
	end
end
addEventHandler("onClientRender", root, delNos)
-------------------------------------------------------------------------------------------------------------------------
local is100 = false
function addNos()
	local veh = getPedOccupiedVehicle(localPlayer)
	if veh and getVehicleUpgradeOnSlot(veh, 8) == 0 then
		addVehicleUpgrade(veh, 1010)
		setVehicleNitroLevel(veh, 1.0)
		is100 = true
	end
end
-------------------------------------------------------------------------------------------------------------------------
function nosFix(k, s)
	local veh = getPedOccupiedVehicle(localPlayer)
	local nos = false
	if veh and getVehicleUpgradeOnSlot(veh, 8) ~= 0 and not isElementFrozen(veh) then
		if is100 == true then
			setElementData(veh, 'nitro', {s==not isVehicleNitroActivated(veh), 0})
			is100 = false
		end
		nos = getElementData(veh, "nitro")
	end
	if nos and nos[2] ~= nil and nos[2] ~= false and nos[2] > 0 then
		setVehicleNitroLevel(veh, nos[2])
	end
end
-------------------------------------------------------------------------------------------------------------------------
addEvent'setNitroType'
addEventHandler('setNitroType', root, function(type)
	nostype = type
end)

addCommandHandler('nostype', function(c, type)
	nostype = type
end)
-------------------------------------------------------------------------------------------------------------------------
-- This syncs nitro states and levels from other clients
addEventHandler('onClientElementDataChange', root, function(name)
	if name ~= 'nitro' or getVehicleOccupant(source) == localPlayer then return end
	local data = getElementData(source, 'nitro')
	setVehicleNitroActivated(source, data[1])
	setVehicleNitroLevel(source, data[2])
end)
-------------------------------------------------------------------------------------------------------------------------
-- Nitro Meter
function nitroShow()
	if isPedInVehicle(localPlayer) then
		local car = getCameraTarget()
		local nitro = getVehicleNitroLevel(getPedOccupiedVehicle(localPlayer))
		if car and getElementType(car) == "vehicle" then
			nitro = getVehicleNitroLevel(car)
		end
		if isPlayerMapVisible() then
			return
		end
		if nitro ~= false and nitro ~= nil and nitro > 0 then
			dxDrawText((math.floor(nitro/1*100)).."% Nitro", x*1463+1, y*134.5+1, x*1455+1, y*74+1, tocolor(0, 0, 0, 255), 1.30*y, "default-bold", "center", "center", false, false, false, false, false)
			
			dxDrawRectangle(x*1396, y*92, x*126, y*23, tocolor(44, 44, 44, 255), false)
			dxDrawRectangle(x*1396, y*92, x*126/10*10*nitro, y*23, tocolor(0, 70, 149, 255), false)
			dxDrawText((math.floor(nitro/1*100)).."% Nitro", x*1463, y*134.5, x*1455, y*74, tocolor(255, 255, 255, 255), 1.30*y, "default-bold", "center", "center", false, false, false, false, false)
		else
			dxDrawText("No Nitro", x*1463+1, y*134.5+1, x*1455+1, y*74+1, tocolor(0, 0, 0, 255), 1.30*y, "default-bold", "center", "center", false, false, false, false, false)
			
			dxDrawRectangle(x*1396, y*92, x*126, y*23, tocolor(44, 44, 44, 255), false)
			dxDrawRectangle(x*1396, y*92, x*126, y*23, tocolor(0, 70, 149, 255), false)
			dxDrawText("No Nitro", x*1463, y*134.5, x*1455, y*74, tocolor(255, 255, 255, 255), 1.30*y, "default-bold", "center", "center", false, false, false, false, false)
		end
	end
end
addEventHandler("onClientRender",getRootElement(),nitroShow)
-------------------------------------------------------------------------------------------------------------------------
function isEventHandlerAdded(sEventName, pElementAttachedTo, func)
 if type(sEventName) == 'string' and
  isElement(pElementAttachedTo) and
  type(func) == 'function'
 then
  local aAttachedFunctions = getEventHandlers(sEventName, pElementAttachedTo)
  if type(aAttachedFunctions) == 'table' and #aAttachedFunctions > 0 then
   for i, v in ipairs(aAttachedFunctions) do
    if v == func then
     return true
    end
   end
  end
 end
 return false
end
--Edited by MegasXLR

server:

addEvent("onPlayerPickUpRacePickup", true)
function onPickupHit(pickupID, pickupType, vehicleModel)
	if pickupType == "nitro" then
		local veh = getPedOccupiedVehicle(source)
		if veh and getVehicleUpgradeOnSlot(veh, 8) ~= 0 and not isElementFrozen(veh) then
			setElementData(veh, 'nitro', {0, 1.0})
		end
	end
end
addEventHandler("onPlayerPickUpRacePickup", root, onPickupHit)

 

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