Jump to content

Problem with setTimer


Tony Brand

Recommended Posts

Posted

hi, i made a function that lets players jump with their car
but i need to add a cooldown for it cause i don't want them to go helping nasa with their cars,just a simple jump with 3 seconds cooldown before next jump,so they can't jump to high
 

function jump()
	local vehicle = getPedOccupiedVehicle(localPlayer)
	if(vehicle)then
		local sx, sy, sz = getElementVelocity(vehicle)
		setElementVelocity(vehicle, sx, sy, sz+0.33)	
	end
end

and if you know how i can make a gui thing called cooldown bar(when this gets filled player knows that his cooldown gets ended and he is abale to jump again) plz tell, tnx

Posted
local sw, sh = guiGetScreenSize()
local jumpTimer = 0
local reload = 3000
function jump()
	if getTickCount() - jumpTimer > reload then
		local vehicle = getPedOccupiedVehicle(localPlayer)
		if(vehicle)then
			local sx, sy, sz = getElementVelocity(vehicle)
			setElementVelocity(vehicle, sx, sy, sz+0.33)	
		end
		jumpTimer = getTickCount()
	end
end

addEventHandler("onClientRender", root,
function()
	local now = getTickCount()
	local endTime = jumpTimer + reload
	local elapsedTime = now - jumpTimer
	local duration = endTime - jumpTimer
	local progress = elapsedTime / duration
	
	local width = interpolateBetween(0, 0, 0, 200, 0, 0, progress, "Linear")
	
	dxDrawRectangle(sw/2 - 100, sh - 100, 200, 20, tocolor(0, 0, 0, 180))
	dxDrawRectangle(sw/2 - 100, sh - 100, width, 20, tocolor(0, 255, 0, 180))
end)
bindKey("lshift", "down", jump)

 

  • Thanks 1
  • Moderators
Posted
local sw, sh = guiGetScreenSize()

local jumpTick = 0
local RELOAD_TIME = 3000


bindKey("lshift", "down", 
    function()
        local now = getTickCount()
        if (now-jumpTick>reload) then
            local vehicle = getPedOccupiedVehicle(localPlayer)
            if (vehicle)then
                local sx, sy, sz = getElementVelocity(vehicle)
                setElementVelocity(vehicle, sx, sy, sz+0.33)	
            end
            jumpTick = now

            addEventHandler("onClientRender", root, drawProgressBar)
        end
    end
)

function drawProgressBar()
    local now = getTickCount()

    if (now-jumpTick>RELOAD_TIME) then -- Only draw the progressbar when it's reloading
        removeEventHandler("onClientRender", root, drawProgressBar)
        return
    end

    local width = 200*(now-jumpTick)/RELOAD_TIME
    
    dxDrawRectangle(sw/2-100, sh-100, 200, 20, tocolor(0, 0, 0, 180))
    dxDrawRectangle(sw/2-100, sh-100, width, 20, tocolor(0, 255, 0, 180)) 
end

There you go :)

  • Thanks 1
Posted (edited)
2 hours ago, Pirulax said:

local sw, sh = guiGetScreenSize()

local jumpTick = 0
local RELOAD_TIME = 3000


bindKey("lshift", "down", 
    function()
        local now = getTickCount()
        if (now-jumpTick>reload) then
            local vehicle = getPedOccupiedVehicle(localPlayer)
            if (vehicle)then
                local sx, sy, sz = getElementVelocity(vehicle)
                setElementVelocity(vehicle, sx, sy, sz+0.33)	
            end
            jumpTick = now

            addEventHandler("onClientRender", root, drawProgressBar)
        end
    end
)

function drawProgressBar()
    local now = getTickCount()

    if (now-jumpTick>RELOAD_TIME) then -- Only draw the progressbar when it's reloading
        removeEventHandler("onClientRender", root, drawProgressBar)
        return
    end

    local width = 200*(now-jumpTick)/RELOAD_TIME
    
    dxDrawRectangle(sw/2-100, sh-100, 200, 20, tocolor(0, 0, 0, 180))
    dxDrawRectangle(sw/2-100, sh-100, width, 20, tocolor(0, 255, 0, 180)) 
end

There you go :)

ERROR: client.lua:10: attempt to compare nil with number
well i fixed it, in line 10 reload must change to RELOAD_TIME
Thanks, and can you tell me how i can get x y z of screen? cause i need to change progress bar place ❤️

Edited by Tony Brand
Posted

well i placed it with several times test, but 2 questions
1-is it any resource or misc that helps me to get sw and sh point of screen?
2- you used

local sw, sh = guiGetScreenSize()

so this will place Progress bar in a place i choosed for all players with any Resolution right?
thank you @Pirulax & @SaNor

  • Moderators
Posted

guiGetScreenSize() just gets the width and height of the screen.
If you want to place something exactly 10px from the bottom of the screen, then you'd use sh-10, if you want to place something 10px from the top of the screen you'd simply use 10, 
And so on.

  • Thanks 1

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