Jump to content

Problem with setVehicleEngineState


Liquidz

Recommended Posts

Hello Community!

I have a little problem with my Vehiclesystem...

I am setting the vehicleEngine Client-side with a bindKey-function.

Now i created this function server-side to do some other stuff but he always return true?

function vehicleTimer() 
    for i,v in ipairs(getElementsByType("vehicle")) do 
        local speed = (function(velX, velY, velZ) return ((velX^2 + velY^2 + velZ^2)^(0.5)) * 180 end)(getElementVelocity(v)) 
         
        local state = getVehicleEngineState(v) 
        if state then  
            -- state is always true 
        end 
    end 
end 

Although my vehicle engine is turned off he always return true... :/

Can somebody help me please? :)

Link to comment

ah ok thanks^^

bindKey("m", "down", 
    function(key, keyState) 
        if key ~= "m" or keyState ~= "down" then return end 
        if not isPedInVehicle(localPlayer) then return end 
        local vehicle = getPedOccupiedVehicle(localPlayer) 
        if localPlayer ~= getVehicleOccupant(vehicle, 0) then return end 
        if engineStarting then return end 
        if getVehicleEngineState(vehicle) then 
            setVehicleEngineState(vehicle, false) 
        else 
            if getElementData(vehicle, "tank") > 0.0 then 
                playSound('engine.wav') 
                engineStarting = true 
                setTimer(startEngine, 1500, 1, vehicle) 
            else 
                playSound('engineNoFuel.wav') 
            end      
        end 
    end 
) 

When i make this server-side now?

How is the easiest way to play the sounds? :D

Link to comment

-- server side:

addEventHandler ( "onPlayerJoin", root, 
    function ( ) 
        bindKey ( source, "M", "down", turnEngine ) 
    end 
) 
  
addEventHandler ( "onResourceStart", resourceRoot, 
    function ( ) 
        for _, player in ipairs ( getElementsByType ( "player" ) ) do 
            bindKey ( player, "M", "down", turnEngine ) 
        end 
    end 
) 
  
engineStarting = { } 
  
function turnEngine ( thePlayer, key, keyState ) 
    if ( key ~= "m" or keyState ~= "down" ) then 
        return 
    end 
  
    if ( not isPedInVehicle ( thePlayer ) ) then 
        return 
    end 
  
    local vehicle = getPedOccupiedVehicle ( thePlayer ) 
    if ( thePlayer ~= getVehicleOccupant ( vehicle, 0 ) ) then 
        return 
    end 
  
    if ( engineStarting [ thePlayer ] ) then 
        return 
    end 
  
    if getVehicleEngineState ( vehicle ) then 
        setVehicleEngineState ( vehicle, false ) 
    else 
        if ( getElementData ( vehicle, "tank" ) > 0.0 ) then 
            triggerClientEvent ( thePlayer, "playSound", thePlayer, 'engine.wav' ) 
            engineStarting [ thePlayer ] = true 
            setTimer ( startEngine, 1500, 1, vehicle ) 
        else 
            triggerClientEvent ( thePlayer, "playSound", thePlayer, 'engineNoFuel.wav' ) 
        end 
    end 
end 

-- client side:

addEvent ( "playSound", true ) 
addEventHandler ( "playSound", root, 
    function ( path ) 
        playSound ( path ) 
    end 
) 

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