Jump to content

[Ajuda] Motor Script


Recommended Posts

Boa Tarde!

O problema do meu script é que quando deixo o motor ligado, ao entrar no carro ele DESLIGA-O, e eu quero que ao entrar no carro ele o deixe ligado!

Script:

local root = getRootElement () 
local thisResourceRoot = getResourceRootElement(getThisResource()) 
  
function thisResourceStart () 
    local players = getElementsByType ( "player" ) 
    for _,this_player in ipairs(players) do 
        bindKey ( this_player, "lctrl", "down", stopEngine, "Motor on/off" ) 
    end 
end 
  
function stopEngine ( player, key, state ) 
    if getPedOccupiedVehicleSeat ( player ) == 0 then 
        local vehicle = getPedOccupiedVehicle ( player ) 
        setVehicleEngineState ( vehicle, not getVehicleEngineState ( vehicle ) ) 
    end 
end 
  
function playerJoined() 
    bindKey ( source, "lctrl", "down", stopEngine, "Motor on/off" ) 
end 
addEventHandler ("onResourceStart", getRootElement(), thisResourceStart) 
  
addEventHandler ( "onVehicleEnter", root, 
    function ( player ) 
        setVehicleEngineState ( source, getElementData ( source, "Motor on/off" ) ) 
    end 
) 
  
 addEventHandler ( "onVehicleExit", stopEngine, 
    function ( player ) 
        setVehicleEngineState ( source, getElementData ( source, "Motor on/off" ) ) 
    end 
) 

Link to comment
local root = getRootElement () 
local thisResourceRoot = getResourceRootElement(getThisResource()) 
  
function thisResourceStart () 
    local players = getElementsByType ( "player" ) 
    for _,this_player in ipairs(players) do 
        bindKey ( this_player, "lctrl", "down", stopEngine, "Motor on/off" ) 
    end 
end 
  
function stopEngine ( player, key, state ) 
    if getPedOccupiedVehicleSeat ( player ) == 0 then 
        local vehicle = getPedOccupiedVehicle ( player ) 
        setVehicleEngineState ( vehicle, not getVehicleEngineState ( vehicle ) ) 
    end 
end 
  
function playerJoined() 
    bindKey ( source, "lctrl", "down", stopEngine, "Motor on/off" ) 
end 
addEventHandler ("onResourceStart", getRootElement(), thisResourceStart) 
  
addEventHandler ( "onVehicleExit", stopEngine, 
    function ( player ) 
        setVehicleEngineState ( source, getElementData ( source, "Motor on/off" ) ) 
    end 
) 

Tente esse.

Link to comment

acredito que esse não seja o código inteiro.

tente trocar

addEventHandler ( "onVehicleExit", stopEngine, 
    function ( player ) 
        setVehicleEngineState ( source, getElementData ( source, "Motor on/off" ) ) 
    end 
) 

para

addEventHandler ( "onVehicleExit",root, 
    function ( player ) 
        setVehicleEngineState ( source, getElementData ( source, "Motor on/off" ) ) 
    end 
) 

Link to comment

O script postado é o script completo.

O problema actual é o seguinte:

- Saiu do veiculo com o motor ligado e ele desliga;

- Entro no veiculo com motor desligado e ele liga;

O que pretendo?:

- Que se sair do carro com o motor ligado ele continua sempre ligado mesmo que não entre ninguém no carro ou quando qualquer jogador entra nele;

- Quando entro no carro e o carro tiver o motor ligado continua ligado, se tiver o motor desligado ele continua desligado

Mas infelizmente não consigo fazer isso :/

Link to comment

Tente:

function thisResourceStart () 
    local players = getElementsByType ( "player" ) 
    for _,this_player in ipairs(players) do 
        bindKey ( this_player, "lctrl", "down", toggleEngine ) 
    end 
end 
addEventHandler ("onResourceStart", resourceRoot, thisResourceStart) 
  
function toggleEngine ( player, key, state ) 
    if getPedOccupiedVehicleSeat ( player ) == 0 then 
        local vehicle = getPedOccupiedVehicle ( player ) 
        setVehicleEngineState ( vehicle, not getVehicleEngineState ( vehicle ) ) 
    end 
end 
  
function playerJoined() 
    bindKey ( source, "lctrl", "down", toggleEngine ) 
end 
addEventHandler("onPlayerJoin", root, playerJoined) 

Link to comment

Se você não quer que outros jogadores controlem o veículo, você deve definir um dono, ou verificar se quem está dirigindo é o jogador que criou o veículo, com getElementSyncer.

Não tenho certeza se é isso o que você quer.

agora quando entro no veículo o motor activa novamente

Tente isto:

local engineState = {} 
function thisResourceStart () 
    local players = getElementsByType ( "player" ) 
    for _,this_player in ipairs(players) do 
        bindKey ( this_player, "lctrl", "down", toggleEngine ) 
    end 
end 
addEventHandler ("onResourceStart", resourceRoot, thisResourceStart) 
  
function toggleEngine ( player, key, state ) 
    if getPedOccupiedVehicleSeat ( player ) == 0 then 
        local vehicle = getPedOccupiedVehicle ( player ) 
        setVehicleEngineState ( vehicle, not getVehicleEngineState ( vehicle ) ) 
        engineState[vehicle] = getVehicleEngineState(vehicle) 
    end 
end 
  
function playerJoined() 
    bindKey ( source, "lctrl", "down", toggleEngine ) 
end 
addEventHandler("onPlayerJoin", root, playerJoined) 
  
addEventHandler("onVehicleEnter", root, 
function () 
    if type(engineState[source]) == "boolean" then 
        getVehicleEngineState(engineState[source]) 
    end 
end) 
addEventHandler("onElementDestroy", root, 
function () 
    if not getElementType(source) == "vehicle" then return end 
    if type(engineState[source]) == "boolean" then 
        engineState[source] = nil 
    end 
end) 

Link to comment

Não é este o script que pretendo, o que pretendo é que qualquer jogador quando entra no veiculo ele continue no seu último estado, ou seja se o último estado do veículo foi desligado o jogador quando entrar o motor continua desligado, se o ultimo estado do veiculo foi ligado quando o jogador entrar o motor continua ligado

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