Jump to content

Tips, examples


Ihnify

Recommended Posts

How well did the script "fuel system" that is is, download auto download auto install Qty benzyl

Then I take this gasoline and show on the client, if there are tips on how to do better, or as an example of how to further make such scripts - write, I will be glad

Study lua +- month

server

  1. local timer = {}
    addEventHandler("onPlayerVehicleEnter", root, function ( theVehicle, seat ) 
        if seat == 0 then 
            for i = 1, #Vehicle.vehicles do
                if theVehicle == Vehicle.vehicleObject [ i ] then
    
                    if(Vehicle.fuel [ i ] <= 0.1) then 
                        return outputChatBox ( "В авто закончился бензин", source )    
                    end
    
                    outputChatBox ( "enter ok", source )
    
                    local thePlayer = source
                    timer [ source ] = setTimer ( function () 
                        if ( Vehicle.fuel [ i ] <= 0.1 ) then
                            outputChatBox ( "В авто закончился бензин", thePlayer )
                            setVehicleEngineState ( theVehicle, false )
                    
                            if isTimer ( sourceTimer ) then  killTimer ( sourceTimer ) end
                        end
                    
                        speedVehicle = getElementSpeed ( theVehicle )
                    
                        kmhFloor = math.floor ( speedVehicle )
    
                        Vehicle.fuel [ i ] = Vehicle.fuel[ i ] - ( speedVehicle + 1 ) / 15000;
    
                        setElementData ( theVehicle, "Fuel",  Vehicle.fuel [ i ] )
                    end, 300, 0 )
                end
            end
        end
    end)
Link to comment
    init = function()
        for index in pairs ( Vehicle.vehicles ) do
            Vehicle.vehicleObject [ index ] = createVehicle( Vehicle.vehicles[index][1], Vector3( Vehicle.vehicles[index][2], Vehicle.vehicles[index][3] , Vehicle.vehicles [ index ] [ 4 ] ), 0, 0, Vehicle.vehicles [ index ] [ 5 ] )

            Vehicle.fuel [ index ] = 1.5
        end
    end;
addEventHandler ( "onResourceStart", resourceRoot, Vehicle.init )

 

Did I make a good script "fuel system"? 

I load the cars and set them a certain amount of fuel.

Then I take the fuel data, and use it on the client, in the speedometer.

Are there standards for writing systems, or give an example of how to use it correctly in the future. I appreciate that.

Learn language lua +- month

Link to comment

Well there's no standards for scripting, there's only logic and basic. However asking here if the script is good isn't going to help you, test the script in MTA server. If it has bugs, fix them or send them here and we will help~

  • Thanks 1
Link to comment

Example:

Server Side:

function sendData()
    triggerClientEvent( source, "fuel", source, Vehicle.fuel [ vehicle ] ) -- you should specify the vehicle...
end
addCommandHandler("sendData", sendData)

Client Side:

local vehicleFuel
addEvent ( "fuel", true )
addEventHandler ( "fuel", root, 
function ( fuel ) 
    vehicleFuel = fuel
end)

so when you use /sendData   , vehicleFuel will be equal to the fuel which came from the server side Vehicle.fuel[vehicle]

Edited by R.I.P Logic
Link to comment

Well setElementData and getElementData is so easier and you can use it to link many scripts but the only issue is if you were lagging ( ping higher than 200 ) the setElementData will take time to function, 

function setData()
    setElementData( vehicle, "fuel", vehicle.fuel[vehicle] )
end
addCommandHandler( "setData", setData)

function getData()
    fuel = getElementData( vehicle, "fuel" )
end
....

setElementData and getElementData you can use it in Server and Client side, but the thing is, it set the data using an element ( player, ped, vehicle ... ) so if you want to setElementData(vehicle... ) then you need to have at the other scripts (server or client) side the same vehicle, example, if you're speaking of yourself( player ), you can use in SERVER SIDE: source 

example:

function test()
  setElementData(source, "testsetsetset", "aaaa" )
end
addCommandHandler( "z", test)

-- Who use the command ' /z ' is the source

or add a variable in function test

function test( player, command)
  setElementData( player, "testsetsetset", "aaaa" )
end
addCommandHandler( "z", test)

-- Who use the command ' /z ' is the player and the command ' z ' but we write it because if you need example to write something to add in setElementData:

function test( player, command, data)
  setElementData( player, "testsetsetset", data )
end
addCommandHandler( "z", test)

-- here we use /z aaaaa
-- So player is the source. command is ' z ' and the data is aaaaa

Now in CLIENT SIDE:

function setData()
    setElementData( localPlayer, "test", "aaaaaaaaaaaaaaa" )
end
addCommandHandler( "z", setData)

-- In client side you can't add player in the function variable there's only localPlayer do specify who used the command, and the variable you add for addCommandHandler are 

function setData( command, data)
    setElementData( localPlayer, "test", data )
end
addCommandHandler( "z", setData)

-- command = ' z ' the difference in server side and client side that in client side you don't need to write the player in variable you always use localPlayer
-- but in server side you cant' write 
-- THINK AS THAT FUNCTION IS IN SERVER SIDE:
function setData( command, data)
    setElementData( source, "test", data )
end
addCommandHandler( "z", setData)

-- so here the variables are command and data, command = source or player and the data = ' z ' what I mean in server side you should write player you can't avoid it, in client side you don't need to write it, hope you understand what I meant, my English writing skils are horrible..

 

Link to comment
1 hour ago, R.I.P Logic said:

Example:

Server Side:


function sendData()
    triggerClientEvent( source, "fuel", source, Vehicle.fuel [ vehicle ] ) -- you should specify the vehicle...
end
addCommandHandler("sendData", sendData)

Client Side:


local vehicleFuel
addEvent ( "fuel", true )
addEventHandler ( "fuel", root, 
function ( fuel ) 
    vehicleFuel = fuel
end)

so when you use /sendData   , vehicleFuel will be equal to the fuel which came from the server side Vehicle.fuel[vehicle]

function sendData( player )
    triggerClientEvent( player, "fuel", player, Vehicle.fuel [ vehicle ] ) -- you should specify the vehicle...
end
addCommandHandler("sendData", sendData)

 

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