Jump to content

Tempo delay em comandos.


Recommended Posts

Posted (edited)

Como eu poderia adicionar delay ao comando neste script?

function repararveiculo ( source )
local accName = getAccountName ( getPlayerAccount ( source ) )
	if isObjectInACLGroup("user." .. accName, aclGetGroup("Staff")) then
	 if isPedInVehicle ( source ) then return end
		local x, y, z = getElementPosition(source) 
		local colcarros = createColSphere ( x, y, z, 2)
		local vehicle = getElementsWithinColShape ( colcarros, "vehicle" )
		destroyElement(colcarros)
		setPedAnimation( source, "CAR", "Fixn_Car_Loop", 4500, true, false, false, false)
		for theKey, veh in ipairs(vehicle) do
		setTimer ( function()
            fixVehicle(veh)

		end, 4500, 1 )	
        end 
	end
 end
addCommandHandler ( "fix", repararveiculo  )

function displayVehicleLoss(loss)
	if getElementHealth(source) <= 255 then 
		setElementHealth(source, 255.5)
		setVehicleEngineState(source, false)
		setVehicleDamageProof(source, true)
	else
	if isVehicleDamageProof(source) then
		setVehicleDamageProof(source, false)
		end
	end
end
addEventHandler("onVehicleDamage", getRootElement(), displayVehicleLoss)

function displayVehicleLoss2()
	if getElementHealth(source) <= 255 then 
		setVehicleEngineState(source, false)
	else
	if isVehicleDamageProof(source) then
		setVehicleDamageProof(source, false)
		end
	end
end
addEventHandler("onVehicleEnter", getRootElement(), displayVehicleLoss2)
		
function DestroyVeiculo ()
destroyElement (veh[source])
end
addEventHandler ("onPlayerLogout", root, DestroyVeiculo)
addEventHandler ("onPlayerQuit", root, DestroyVeiculo)

Tentei usar o setTimer mas não consegui muito bem, alguém poderia colocar este script com o delay já ou poderia dar um exemplo por favor?

Edited by Pacheco465
Posted

local tPlayers = {} -- tabela global, esta irá armazenar os jogadores com o tempo que usou o comando
local tempo = 5000 -- 5 segundos em milissegundos

function repararveiculo ( source )
    local accName = getAccountName ( getPlayerAccount ( source ) )
    if isObjectInACLGroup("user." .. accName, aclGetGroup("Staff")) then
        if isPedInVehicle ( source ) then return end
        -- verificar se existe o tempo vinculado ao jogador; verificar quanto tempo passou desde que usou da última vez
        if tPlayers[source] and (getTickCount(source) - tPlayers[source]) < tempo then
            -- parar a execução aqui
            return outputChatBox( "Aguarde um tempo para usar este comando novamente", source, 230,0,0 )
        end
        tPlayers[source] = getTickCount(source) -- armazenar o tempo em que o jogador usou o comando

        local x, y, z = getElementPosition(source) 
        local colcarros = createColSphere ( x, y, z, 2)
        local vehicle = getElementsWithinColShape ( colcarros, "vehicle" )
        destroyElement(colcarros)
        setPedAnimation( source, "CAR", "Fixn_Car_Loop", 4500, true, false, false, false)
        for theKey, veh in ipairs(vehicle) do
            setTimer ( function()
                fixVehicle(veh)
            end, 4500, 1 )	
        end
    end
 end
addCommandHandler ( "fix", repararveiculo  )

-- otimização
addEventHandler("onPlayerQuit", root, function()
    -- remover da tabela global ao sair do servidor
    if tPlayers[source] then
        tPlayers[source] = nil
    end
end)

Leia os comentários do código.

Please do not PM me with scripting related question nor support, use the forums instead.

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