In the referred video, there's a 2v2 volleyball mini-game in a custom location with player UIs for scores and team identification. Here's how we can update some functionalities from the previous example to better resemble the video:
- Detecting player interactions with the ball.
Update the
MonitorBallInteractions function to use the melee attack event for hitting the ball, instead of a specific key press:
function MonitorBallInteractions(ball)
local ballEntity = NetToPed(ball)
while true do
Citizen.Wait(0)
local playerPed = PlayerPedId()
local ballCoords = GetEntityCoords(ballEntity)
if GetDistanceBetweenCoords(GetEntityCoords(playerPed), ballCoords, true) < 2.0 then
if IsPedInMeleeCombat(playerPed) then -- Change from specific key press to melee attack event
local forceMultiplier = 5.0 -- Adjust the force of the hit
local playerForwardVector = GetEntityForwardVector(playerPed)
ApplyForceToEntity(ballEntity, 3, playerForwardVector * forceMultiplier, 0, 0, 0, 0, 0, 0, false, false, true, true, false, true)
end
end
end
end
With this change, the player can hit the ball with any melee attack instead of using a specific key press.
- Registering player teams and scores
First, let's update the team identification by adding colored icons above each player's head representing their team. We'll use the
DrawMarker native function.
Add this function to main.lua:
function DrawTeamMarker(teamName, ped)
local markerType = 27
local scale = vector3(0.5, 0.5, 0.5)
local color = nil
if teamName == "teamA" then
color = { r = 0, g = 0, b = 255, a = 255 }
elseif teamName == "teamB" then
color = { r = 255, g = 0, b = 0, a = 255 }
else
return
end
local pedCoords = GetEntityCoords(ped)
DrawMarker(markerType, pedCoords.x, pedCoords.y, pedCoords.z + 1.0, 0, 0, 0, 0, 0, 0, scale, scale, scale, color.r, color.g, color.b, color.a, false, true, 2, true, false, false, false)
end
Then, update the MonitorBallInteractions function to include the new DrawTeamMarker function to show each player's team affiliation:
function MonitorBallInteractions(ball)
local ballEntity = NetToPed(ball)
while true do
Citizen.Wait(0)
-- New functionality: Draw team markers above players' heads
for teamName, team in pairs(teamData) do
for _, playerName in ipairs(team.players) do
for _, playerId in ipairs(GetActivePlayers()) do
local ped = GetPlayerPed(playerId)
if playerName == GetPlayerName(playerId) then
DrawTeamMarker(teamName, ped)
end
end
end
end
-- ...
-- Rest of the function code
end
end
Now, we will draw the current scores on screen for each team. Add this function to main.lua:
function DrawScores()
SetTextFont(0)
SetTextProportional(1)
SetTextScale(0.0, 0.5)
SetTextColour(255, 255, 255, 255)
SetTextDropshadow(0, 0, 0, 0, 255)
SetTextEdge(1, 0, 0, 0, 255)
SetTextDropShadow()
SetTextOutline()
local scoreA = string.format("Team A: %d", teamData.teamA.score)
local scoreB = string.format("Team B: %d", teamData.teamB.score)
local finalText = scoreA .. " " .. scoreB
BeginTextCommandDisplayText("STRING")
AddTextComponentSubstringPlayerName(finalText)
EndTextCommandDisplayText(0.5, 0.05)
end
Update the MonitorBallInteractions function again to include the new DrawScores function, which will constantly update and display the scores:
function MonitorBallInteractions(ball)
local ballEntity = NetToPed(ball)
while true do
Citizen.Wait(0)
-- Draw team markers and scores
for teamName, team in pairs(teamData) do
for _, playerName in ipairs(team.players) do
for _, playerId in ipairs(GetActivePlayers()) do
local ped = GetPlayerPed(playerId)
if playerName == GetPlayerName(playerId) then
DrawTeamMarker(teamName, ped)
end
end
end
end
DrawScores()
-- ...
-- Rest of the function code
end
end
These examples extend the existing implementation to better resemble the video demonstration. The functionality is still basic and can be further enhanced by adding proper game rules, a win/loss system, detecting actual completed gameplay actions instead of pressing a specific key, and more optimized code. Please note that it is still a starting point and may need more work to have a full-fledged minigame.