Jump to content

Fuel system and money


Recommended Posts

Hello please I need help, I have got a fuel system but players can refuel without money.

Fuel Stations

sx,sy = guiGetScreenSize() 
  
local punktyTankowania = { 
    {-2029.52, 156.77, 28.84}, 
    {-2023.89, 156.69, 28.84}, 
    {-2405.73, 975.74, 45.30}, 
    {-2416.30, 974.97, 45.30}, 
    {-2640.45, 1375.09, 7.15}, 
    {-1675.46,413.69,7.21}, 
    {-1328.36,2677.03,50.09}, 
    {612.50,1694.81,7.03}, 
    {2115.18,919.53,10.85}, 
    {1383.09,460.70,20.12}, 
    {1004.86,-939.32,42.22}, 
} 
  
for i,v in ipairs(punktyTankowania) do 
    zpwp_marker = createMarker(v[1], v[2], v[3]-1.0, "cylinder", 4, 0, 0, 0, 0) 
    setElementData(zpwp_marker, "stacja_benzynowa", true) 
end 
  
zpwp_okno = guiCreateWindow(199/1280*sx, 191/720*sy, 872/1280*sx, 294/720*sy, "Stacja benzynowa", false) 
guiWindowSetSizable(zpwp_okno, false) 
guiSetVisible(zpwp_okno, false) 
  
zpwp_warning = guiCreateLabel(17/1280*sx, 18/720*sy, 845/1280*sx, 16/720*sy, "", false, zpwp_okno) 
guiLabelSetColor(zpwp_warning, 183, 0, 0) 
guiLabelSetHorizontalAlign(zpwp_warning, "center", false)     
         
zpwp_info = guiCreateLabel(211/1280*sx, 81/720*sy, 477/1280*sx, 15/720*sy, "Stacja wita, poniżej jest przedstawiony cennik paliwa, cennik może ulec zmianie.", false, zpwp_okno) 
guiSetFont(zpwp_info, "default-bold-small") 
  
zpwp_stan_l = guiCreateLabel(17/1280*sx, 48/720*sy, 111/1280*sx, 15/720*sy, "Obecny stan paliwa:", false, zpwp_okno) 
guiSetFont(zpwp_stan_l, "default-bold-small") 
  
zpwp_stan = guiCreateProgressBar(154/1280*sx, 38/720*sy, 708/1280*sx, 30/720*sy, false, zpwp_okno) 
guiProgressBarSetProgress(zpwp_stan, 0) 
  
zpwp_cena = guiCreateLabel(365/1280*sx, 106/720*sy, 143/1280*sx, 16/720*sy, "Cena za jeden litr: 3 ZŁ", false, zpwp_okno) 
  
zpwp_ilosc = guiCreateLabel(318/1280*sx, 132/720*sy, 208/1280*sx, 15/720*sy, "Wpisz ile liter paliwa, chcesz zakupić:", false, zpwp_okno) 
zpwp_ilosc_e = guiCreateEdit(536/1280*sx, 127/720*sy, 40/1280*sx, 26/720*sy, "", false, zpwp_okno) 
  
zpwp_zakup = guiCreateButton(27/1280*sx, 171/720*sy, 820/1280*sx, 113/720*sy, "TANKUJ", false, zpwp_okno) 
  
addEventHandler("onClientMarkerHit", resourceRoot, function(el, md) 
    if not getElementData(source, "stacja_benzynowa") then return end 
    if el ~= localPlayer then return end 
    if guiGetVisible(zpwp_okno) == false then 
        local pojazd = getPedOccupiedVehicle(el) 
        if not pojazd then return end 
         
        local paliwo = getElementData(pojazd, "pojazd_paliwo" ) 
         
        showCursor(true, false) 
        guiSetVisible(zpwp_okno, true) 
        guiProgressBarSetProgress(zpwp_stan, paliwo) 
    end 
end) 
  
addEventHandler("onClientMarkerLeave", resourceRoot, function(el, md) 
    if not getElementData(source, "stacja_benzynowa") then return end 
    if el ~= localPlayer then return end 
    if guiGetVisible(zpwp_okno) == true then 
        showCursor(false) 
        guiSetVisible(zpwp_okno, false) 
    end 
end) 
  
addEventHandler("onClientGUIClick", zpwp_zakup, function() 
    local pojazd = getPedOccupiedVehicle(localPlayer) 
    if not pojazd then return end -- nie powinno się wydarzyć 
    local stan = getElementData(pojazd, "pojazd_paliwo") 
    if not stan then return end -- nie powinno się wydarzyć 
    if not tonumber(guiGetText(zpwp_ilosc_e)) then return end 
    local ile = stan + guiGetText(zpwp_ilosc_e) 
    local koszt = guiGetText(zpwp_ilosc_e) * 3 
    if ile < stan then 
        setTimer(guiSetText, 2000, 1, zpwp_warning, "") 
        guiSetText(zpwp_warning, "Podałeś złą wartość paliwa.") 
        return 
    end 
    if ile < 101 then 
        if getPlayerMoney(localPlayer) > koszt then 
            showCursor(false) 
            guiSetVisible(zpwp_okno, false) 
            outputChatBox("* Pojazd został zatankowany...") 
            setElementData(pojazd, "pojazd_paliwo", tonumber(ile)) 
            triggerServerEvent("takePlayerMoney", localPlayer, koszt) 
        else 
            setTimer(guiSetText, 2000, 1, zpwp_warning, "") 
            guiSetText(zpwp_warning, "Nie posiadasz wystarczającej ilości pieniędzy.") 
        end 
    else 
        setTimer(guiSetText, 2000, 1, zpwp_warning, "") 
        guiSetText(zpwp_warning, "Maxymalnie pojazd może mieć 100% litrów paliwa") 
    end 
end) 

Fuel system

local czas = getTickCount() 
  
isBike = { 
    [509]=true, --Bike 
    [481]=true, --BMX 
    [510]=true, --Mountain bike 
} 
  
function odejmijPaliwo(pojazd) 
    if getTickCount() - czas > 150 then 
        czas = getTickCount() 
        local paliwo = getElementData(pojazd, "pojazd_paliwo") or 0 
        local vx,vy,vz = getElementVelocity(pojazd) 
        local spd = ((vx^2+vy^2+vz^2)^(0.5)/150) 
        if spd > 0 then 
            paliwo = paliwo-spd*2 
            setElementData(pojazd, "pojazd_paliwo", paliwo) 
        end 
    end 
end 
  
addEventHandler("onClientRender", root, function() 
    local pojazd = getPedOccupiedVehicle(localPlayer) 
    if not pojazd then return end 
    if isBike[getElementModel(pojazd)] then return end 
    if not getVehicleEngineState(pojazd) then return end 
    if getVehicleController(pojazd) ~= localPlayer then return end 
    odejmijPaliwo(pojazd) 
end) 
  
function zgasSilnik() 
    for i,v in ipairs(getElementsByType("vehicle")) do 
    local data = getElementData(v, "pojazd_paliwo") 
        if data then 
        if tonumber(data) < 0 then 
            setVehicleEngineState(v, false) 
        end 
end 
    end 
end 
setTimer(zgasSilnik, 1000, 999999999)  

Client

sw,sh = guiGetScreenSize() 
  
isBike = { 
    [509]=true, --Bike 
    [481]=true, --BMX 
    [510]=true, --Mountain bike 
} 
  
addEventHandler("onClientRender", root, function() 
    if isPlayerMapVisible() then return end 
    local pojazd = getPedOccupiedVehicle(localPlayer) 
    if pojazd then 
        if isBike[getElementModel(pojazd)] then return end 
        local sprawdzpaliwo = tonumber(getElementData(pojazd, "pojazd_paliwo")) 
        local sprawdzprzebieg = tonumber(getElementData(pojazd, "pojazd_przebieg")) or 000 
        local id = getElementData(pojazd, "car_id") or "Ten Pojazd Nie Posiada Id." 
        local przebieg = math.floor(sprawdzprzebieg) or 0 
        if #tostring(przebieg) == 1 then przebieg = "0000"..przebieg 
        elseif #tostring(przebieg) == 2 then przebieg = "000"..przebieg 
        elseif #tostring(przebieg) == 3 then przebieg = "00"..przebieg 
        elseif #tostring(przebieg) == 4 then przebieg = "0"..przebieg 
        elseif #tostring(przebieg) == 5 then przebieg = ""..przebieg 
        elseif #tostring(przebieg) == 8 then przebieg = przebieg end 
        local paliwo = math.floor(sprawdzpaliwo) or 60 
         
        local sx, sy, sz = getElementVelocity(pojazd) 
        local predkosc = math.ceil(((sx^2+sy^2+sz^2)^(0.5)) * 161) 
         
        dxDrawRectangle(1142/1440*sw, 644/900*sh, 288/1440*sw, 246/900*sh, tocolor(0, 0, 0, 217), false) 
        dxDrawRectangle(1132/1440*sw, 615/900*sh, 302/1440*sw, 29/900*sh, tocolor(0, 93, 2, 217), false) 
        dxDrawText("Id Twojego Pojazdu", 1132/1440*sw, 590/900*sh, 1434/1440*sw, 621/900*sh, tocolor(255, 255, 255, 255), 2.00, "default-bold", "center", "center", false, false, false, false, false) 
        dxDrawText("Prędkość", 1146/1440*sw, 654/900*sh, 1424/1440*sw, 679/900*sh, tocolor(255, 255, 255, 255), 2.00, "default-bold", "center", "center", false, false, false, false, false) 
        dxDrawText(""..predkosc.." km/h", 1233/1440*sw, 689/900*sh, 1343/1440*sw, 715/900*sh, tocolor(0, 93, 2, 217), 1.50, "default", "center", "center", false, false, false, false, false) 
        dxDrawText("Przebieg", 1146/1440*sw, 725/900*sh, 1424/1440*sw, 750/900*sh, tocolor(255, 255, 255, 255), 2.00, "default-bold", "center", "center", false, false, false, false, false) 
        dxDrawText(""..przebieg.." km/h", 1233/1440*sw, 760/900*sh, 1343/1440*sw, 786/900*sh, tocolor(0, 93, 2, 217), 1.50, "default", "center", "center", false, false, false, false, false) 
        dxDrawText("Paliwo", 1146/1440*sw, 796/900*sh, 1424/1440*sw, 821/900*sh, tocolor(255, 255, 255, 255), 2.00, "default-bold", "center", "center", false, false, false, false, false) 
        dxDrawText(""..paliwo.." L", 1233/1440*sw, 831/900*sh, 1343/1440*sw, 857/900*sh, tocolor(0, 93, 2, 217), 1.50, "default", "center", "center", false, false, false, false, false) 
        dxDrawText(""..id.."", 1175/1440*sw, 626/900*sh, 1420/1440*sw, 644/900*sh, tocolor(217, 225, 0, 255), 1.00, "pricedown", "center", "center", false, false, false, false, false) 
end 
end) 
  
  
--[[ 
--        dxDrawRectangle(947/1280*sw, 549/720*sh, 333/1280*sw, 123/720*sh, tocolor(90,90,90, 75), true) 
--        dxDrawText("  Przebieg: "..przebieg.."", 957/1280*sw, 594/720*sh, 1243/1280*sw, 622/720*sh, tocolor(255, 255, 255, 255), 2.50, "default-small", "left", "top", false, false, true, false, false) 
--        dxDrawText("Paliwo: "..paliwo or 60 .."%", 954/1280*sw, 556/720*sh, 1240/1280*sw, 584/720*sh, tocolor(255, 255, 255, 255), 2.50, "default-small", "left", "top", false, false, true, false, false) 
--        dxDrawText("    Predkosc: "..predkosc.." km/h", 954/1280*sw, 632/720*sh, 1240/1280*sw, 660/720*sh, tocolor(255, 255, 255, 255), 2.50, "default-small", "left", "top", false, false, true, false, false) 
    
        dxDrawRectangle(1146/1440*sw, 682/900*sh, 294/1440*sw, 210/900*sh, tocolor(0, 0, 0, 200), false) 
        dxDrawRectangle(1144/1440*sw, 645/900*sh, 296/1440*sw, 38/900*sh, tocolor(0, 0, 0, 255), false) 
        dxDrawRectangle(1145/1440*sw, 646/900*sh, 294/1440*sw, 36/900*sh, tocolor(40, 40, 40, 200), false) 
        dxDrawText("Pojazd:", 1145/1440*sw, 646/900*sh, 1440/1440*sw, 682/900*sh, tocolor(255, 255, 255, 255), 1.00, "default", "center", "center", false, false, false, false, false) 
        dxDrawText("Prędkość:", 1145/1440*sw, 682/900*sh, 1440/1440*sw, 718/900*sh, tocolor(255, 255, 255, 255), 1.00, "pricedown", "center", "center", false, false, false, false, false) 
        dxDrawText("Paliwo:", 1146/1440*sw, 822/900*sh, 1441/1440*sw, 858/900*sh, tocolor(255, 255, 255, 255), 1.00, "pricedown", "center", "center", false, false, false, false, false) 
        dxDrawText(""..predkosc.." KM/H", 1146/1440*sw, 718/900*sh, 1441/1440*sw, 752/900*sh, tocolor(255, 255, 255, 255), 1.00, "default", "center", "center", false, false, false, false, false) 
        dxDrawText(""..przebieg.." KM", 1146/1440*sw, 788/900*sh, 1441/1440*sw, 822/900*sh, tocolor(255, 255, 255, 255), 1.00, "default", "center", "center", false, false, false, false, false) 
        dxDrawText(""..paliwo.."L", 1146/1440*sw, 858/900*sh, 1441/1440*sw, 892/900*sh, tocolor(255, 255, 255, 255), 1.00, "default", "center", "center", false, false, false, false, false) 
        dxDrawText("Przebieg:", 1145/1440*sw, 752/900*sh, 1440/1440*sw, 788/900*sh, tocolor(255, 255, 255, 255), 1.00, "pricedown", "center", "center", false, false, false, false, false) 
--]] 

Link to comment
I don't see you actually change the players money at all here. You can do that using:
setPlayerMoney 

bad idea, if player got insufficient funds it would still work and would set his money to the cost of $, and if player got high money , it would set his money to the cost of $.

use

takePlayerMoney 

use this to get player's money

getPlayerMoney 

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