Certainly! Below is a step-by-step guide on how to modify your MTA Lua scripts to:
-
Add variables on the client-side to track how much the player has earned and how many orders they've completed during their job session.
-
Display this information on the screen using dxDrawText, over a dark transparent rectangle in a corner of the screen, whenever the player starts the job.
Step 1: Define the Variables on the Client Side
In your client.lua script, we'll begin by adding variables to store the earnings and the number of orders completed. We'll also add a flag isOnJob to track if the player is currently on the job.
Add these lines at the top of your client.lua file, near other variable declarations:
-- Variables
local pizzaMarker = nil
local pizzaObjects = {}
local hasPizza = false
local deliveryMarker = nil
local deliveryNPC = nil
local currentOrder = nil
local showText = false
local screenWidth, screenHeight = guiGetScreenSize()
-- New variables for tracking earnings and orders
local moneyEarned = 0
local ordersCompleted = 0
local isOnJob = false -- To track if the player is on the job
Step 2: Initialize Variables When the Player Starts the Job
When the player starts the pizza delivery job, we'll reset moneyEarned and ordersCompleted to zero and set isOnJob to true. We'll also add the drawJobInfo function to the onClientRender event to start displaying the information.
Modify the startPizzaJob event handler in client.lua as follows:
addEvent("startPizzaJob", true)
addEventHandler("startPizzaJob", resourceRoot, function()
createPizzaMarker()
moneyEarned = 0
ordersCompleted = 0
isOnJob = true
addEventHandler("onClientRender", root, drawJobInfo)
end)
Step 3: Handle When the Player Quits the Job
We need to reset the variables and stop displaying the information when the player quits the job. Since the server handles job termination, we'll modify the server script to notify the client.
In your server.lua, add a triggerClientEvent when the player quits the job, inside the onJobMarkerHit function:
-- Player quits the job
local scooter = getPlayerScooter(player)
if scooter then
destroyElement(scooter)
end
setElementData(player, "job", nil)
outputChatBox("Вы уволились с работы развозчика пиццы.", player)
-- Notify client to stop the job
triggerClientEvent(player, "stopPizzaJob", resourceRoot)
Back in client.lua, add an event handler for stopPizzaJob:
addEvent("stopPizzaJob", true)
addEventHandler("stopPizzaJob", resourceRoot, function()
isOnJob = false
removeEventHandler("onClientRender", root, drawJobInfo)
moneyEarned = 0
ordersCompleted = 0
end)
Also, modify the onStopResource function in client.lua to handle resource stops properly:
function onStopResource()
toggleControl("sprint", true)
toggleControl("jump", true)
toggleControl("crouch", true)
toggleControl("fire", true)
for _, pizzaObject in ipairs(pizzaObjects) do
if isElement(pizzaObject) then
destroyElement(pizzaObject)
end
end
pizzaObjects = {}
hasPizza = false
if isElement(deliveryMarker) then
destroyElement(deliveryMarker)
end
if isElement(deliveryNPC) then
destroyElement(deliveryNPC)
end
deliveryMarker = nil
deliveryNPC = nil
setElementData(localPlayer, "has_order", false)
isOnJob = false
removeEventHandler("onClientRender", root, drawJobInfo)
moneyEarned = 0
ordersCompleted = 0
end
addEventHandler("onClientResourceStop", resourceRoot, onStopResource)
Step 4: Create the Function to Draw the Information on the Screen
We'll define a function drawJobInfo that uses dxDrawRectangle and dxDrawText to display the earnings and orders completed over a dark transparent rectangle.
Add this function to your client.lua:
function drawJobInfo()
if isOnJob then
-- Define the position and size of the rectangle
local x = screenWidth - 310
local y = 10
local width = 300
local height = 50
-- Draw the background rectangle (black with 150 alpha)
dxDrawRectangle(x, y, width, height, tocolor(0, 0, 0, 150))
-- Prepare the text to display
local text = "Заработано: $" .. tostring(moneyEarned) .. "\nВыполнено заказов: " .. tostring(ordersCompleted)
-- Draw the text inside the rectangle
dxDrawText(text, x + 10, y + 10, x + width - 10, y + height - 10, tocolor(255, 255, 255, 255), 1.0, "default-bold", "left", "top")
end
end
Step 5: Update Variables When an Order is Completed
We'll update moneyEarned and ordersCompleted when the player successfully completes a delivery. Since the server handles the actual reward (which may vary), it's best to have the server inform the client about the amount earned.
Modify the server's onPlayerCompleteOrder function in server.lua to send the amount to the client:
addEvent("onPlayerCompleteOrder", true)
addEventHandler("onPlayerCompleteOrder", root, function()
local player = client
local amount = 100 -- Reward for delivery
givePlayerMoney(player, amount)
outputChatBox("Вы успешно доставили пиццу и получили $" .. amount .. "!", player)
setElementData(player, "has_order", false)
-- Reset scooter data
local scooter = getPlayerScooter(player)
if scooter then
setElementData(scooter, "has_pizza", false)
end
-- Notify client about the earnings
triggerClientEvent(player, "onClientUpdateEarnings", resourceRoot, amount)
end)
Then, in your client.lua, handle this event to update the variables:
addEvent("onClientUpdateEarnings", true)
addEventHandler("onClientUpdateEarnings", resourceRoot, function(amount)
moneyEarned = moneyEarned + amount
ordersCompleted = ordersCompleted + 1
end)
Also, make sure to remove any variable updates from the onDeliveryMarkerHit function since we're now updating them via the server event:
function onDeliveryMarkerHit(hitPlayer, matchingDimension)
if hitPlayer == localPlayer and getElementData(localPlayer, "has_order") then
destroyElement(deliveryMarker)
deliveryMarker = nil
destroyElement(deliveryNPC)
deliveryNPC = nil
setElementData(localPlayer, "has_order", false)
triggerServerEvent("onPlayerCompleteOrder", resourceRoot)
outputChatBox("Вы доставили пиццу!", 0, 255, 0)
-- Variables are now updated via the 'onClientUpdateEarnings' event
end
end
Step 6: Testing
With these changes, when the player starts the pizza delivery job:
- The earnings (
moneyEarned) and orders completed (ordersCompleted) variables are initialized.
- The
drawJobInfo function is added to the onClientRender event to display the information.
- When the player completes an order, the server informs the client of the amount earned, and the client updates the variables accordingly.
- When the player quits the job or the resource stops, the information is no longer displayed, and variables are reset.
Complete Code Changes
Updated client.lua:
-- client.lua
-- Variables
local pizzaMarker = nil
local pizzaObjects = {}
local hasPizza = false
local deliveryMarker = nil
local deliveryNPC = nil
local currentOrder = nil
local showText = false
local screenWidth, screenHeight = guiGetScreenSize()
-- New variables for tracking earnings and orders
local moneyEarned = 0
local ordersCompleted = 0
local isOnJob = false -- To track if the player is on the job
-- Pizzeria marker position
local pizzaMarkerPosition = Vector3(-1983.57, 182.51, 26.68)
-- Function to create the pizza pickup marker
function createPizzaMarker()
if not pizzaMarker then
pizzaMarker = createMarker(pizzaMarkerPosition, "cylinder", 1.5, 255, 255, 0, 150)
addEventHandler("onClientMarkerHit", pizzaMarker, onPlayerHitPizzaMarker)
end
end
-- Start of the pizza delivery job
addEvent("startPizzaJob", true)
addEventHandler("startPizzaJob", resourceRoot, function()
createPizzaMarker()
moneyEarned = 0
ordersCompleted = 0
isOnJob = true
addEventHandler("onClientRender", root, drawJobInfo)
end)
-- Stop the pizza delivery job
addEvent("stopPizzaJob", true)
addEventHandler("stopPizzaJob", resourceRoot, function()
isOnJob = false
removeEventHandler("onClientRender", root, drawJobInfo)
moneyEarned = 0
ordersCompleted = 0
end)
-- Function to draw the job information on the screen
function drawJobInfo()
if isOnJob then
-- Define the position and size of the rectangle
local x = screenWidth - 310
local y = 10
local width = 300
local height = 50
-- Draw the background rectangle (black with 150 alpha)
dxDrawRectangle(x, y, width, height, tocolor(0, 0, 0, 150))
-- Prepare the text to display
local text = "Заработано: $" .. tostring(moneyEarned) .. "\nВыполнено заказов: " .. tostring(ordersCompleted)
-- Draw the text inside the rectangle
dxDrawText(text, x + 10, y + 10, x + width - 10, y + height - 10, tocolor(255, 255, 255, 255), 1.0, "default-bold", "left", "top")
end
end
-- Rest of your client.lua code stays the same...
-- Add the event handler for updating earnings
addEvent("onClientUpdateEarnings", true)
addEventHandler("onClientUpdateEarnings", resourceRoot, function(amount)
moneyEarned = moneyEarned + amount
ordersCompleted = ordersCompleted + 1
end)
-- Modify the onDeliveryMarkerHit function
function onDeliveryMarkerHit(hitPlayer, matchingDimension)
if hitPlayer == localPlayer and getElementData(localPlayer, "has_order") then
destroyElement(deliveryMarker)
deliveryMarker = nil
destroyElement(deliveryNPC)
deliveryNPC = nil
setElementData(localPlayer, "has_order", false)
triggerServerEvent("onPlayerCompleteOrder", resourceRoot)
outputChatBox("Вы доставили пиццу!", 0, 255, 0)
-- Variables are now updated via the 'onClientUpdateEarnings' event
end
end
-- Ensure to reset variables in onStopResource
function onStopResource()
-- Existing code...
isOnJob = false
removeEventHandler("onClientRender", root, drawJobInfo)
moneyEarned = 0
ordersCompleted = 0
end
addEventHandler("onClientResourceStop", resourceRoot, onStopResource)
Updated server.lua:
-- server.lua
-- Existing code...
-- Modify the onJobMarkerHit function to notify the client when quitting the job
function onJobMarkerHit(hitElement)
if getElementType(hitElement) == "player" then
local player = hitElement
local job = getElementData(player, "job")
if job == "pizza_delivery" then
-- Player quits the job
local scooter = getPlayerScooter(player)
if scooter then
destroyElement(scooter)
end
setElementData(player, "job", nil)
outputChatBox("Вы уволились с работы развозчика пиццы.", player)
-- Notify client to stop the job
triggerClientEvent(player, "stopPizzaJob", resourceRoot)
else
-- Player starts the job
-- Existing code...
triggerClientEvent(player, "startPizzaJob", resourceRoot)
end
end
end
-- Modify the onPlayerCompleteOrder function to send earnings to the client
addEvent("onPlayerCompleteOrder", true)
addEventHandler("onPlayerCompleteOrder", root, function()
local player = client
local amount = 100 -- Reward for delivery
givePlayerMoney(player, amount)
outputChatBox("Вы успешно доставили пиццу и получили $" .. amount .. "!", player)
setElementData(player, "has_order", false)
-- Reset scooter data
local scooter = getPlayerScooter(player)
if scooter then
setElementData(scooter, "has_pizza", false)
end
-- Notify client about the earnings
triggerClientEvent(player, "onClientUpdateEarnings", resourceRoot, amount)
end)
-- Rest of your server.lua code stays the same...
Final Notes
-
Make sure to test the scripts thoroughly to ensure the variables are updating correctly and the information displays and hides as expected.
-
Adjust the positions and styles of the text and rectangle in the drawJobInfo function if needed to better fit your game's UI.
-
Remember to handle any edge cases, such as the player disconnecting while on the job or other scenarios specific to your game.
By following these steps, you've added client-side variables to track how much the player has earned and how many orders they've completed, and you've displayed this information on the screen when the player is working as a pizza delivery driver in your MTA Lua script.