actions.lua
OpenStreetRaceMenu = function(bet)
local elements = {}
if bet == nil then
bet = 100
end
if not hasJoinedRace then
table.insert(elements, { ["label"] = "Välj Satsning - (" .. bet .. " SEK)", ["value"] = "place_wager" })
table.insert(elements, { ["label"] = "Anmäl dig", ["value"] = "join_race" })
else
table.insert(elements, { ["label"] = "-- Va kvar här tills 20:00 --" })
table.insert(elements, { ["label"] = "Lämna racet", ["value"] = "leave_race" })
end
table.insert(elements, { ["label"] = "Kolla anmälda", ["value"] = "check_race_members" })
ESX.UI.Menu.Open('default', GetCurrentResourceName(), "streetrace_menu",
{
title = "Streetrace",
align = "center",
elements = elements
},
function(data, menu)
local value = data.current.value
if value == "place_wager" then
ESX.Dialog("Hur mycekt vill du satsa på dig själv?", function(wager)
local wager = tonumber(wager)
if wager == nil then
ESX.ShowNotification("Du kan bara satsa ~g~pengar.")
return
end
if wager < 100 then
ESX.ShowNotification("Minsta satsning är ~g~100 SEK")
return
end
if wager > 50000 then
ESX.ShowNotification("Högsta satsning är ~g~50000 SEK")
return
end
menu.close()
OpenStreetRaceMenu(wager)
end)
elseif value == "join_race" then
ChooseVehicle(function(vehicleChosen)
ESX.TriggerServerCallback("unrp_streetrace:joinRace", function(joinedRace, err)
if joinedRace then
ESX.ShowNotification("Du ~g~anmälde~s~ dig för ~g~" .. bet .. " SEK")
hasJoinedRace = true
OpenStreetRaceMenu()
else
if err == "too_many" then
ESX.ShowNotification("Det är för ~r~många~s~ som har ~b~anmält~s~ sig.")
else
ESX.ShowNotification("Du har inte ~r~råd~s~ det saknas ~g~" .. err .. " SEK")
end
end
end, bet, vehicleChosen)
end)
elseif value == "leave_race" then
ESX.TriggerServerCallback("unrp_streetrace:leaveRace", function(leftRace, retrievedMoney)
if leftRace then
ESX.ShowNotification("Du ~r~lämnade~s~ och fick tillbaks ~g~" .. retrievedMoney .. " SEK")
hasJoinedRace = false
OpenStreetRaceMenu()
else
ESX.ShowNotification("Du var inte ens med i racet...")
hasJoinedRace = false
OpenStreetRaceMenu()
end
end)
elseif value == "check_race_members" then
CheckRaceMembers()
end
end, function(data, menu)
menu.close()
end)
end
ChooseVehicle = function(cb)
local elements = {}
ESX.TriggerServerCallback("unrp_streetrace:fetchVehicles", function(vehicleArray)
if vehicleArray ~= nil and #vehicleArray > 0 then
for i = 1, #vehicleArray do
local vehicle = vehicleArray[i]
local vehicleProps = json.decode(vehicle["vehicle"])
if GetLabelText(GetDisplayNameFromVehicleModel(vehicleProps["model"])) ~= "NULL" then
vehicleProps["label"] = GetLabelText(GetDisplayNameFromVehicleModel(vehicleProps["model"]))
else
vehicleProps["label"] = GetDisplayNameFromVehicleModel(vehicleProps["model"])
end
table.insert(elements, { ["label"] = vehicleProps["label"] .. " - " .. vehicle["plate"], ["value"] = vehicleProps })
end
ESX.UI.Menu.Open('default', GetCurrentResourceName(), "streetrace_menu_vehicle_chooser",
{
title = "Välj ditt fordon du vill köra i",
align = "center",
elements = elements
},
function(data, menu)
local value = data.current.value
if value ~= nil then
cb(value)
ESX.UI.Menu.CloseAll()
end
end, function(data, menu)
menu.close()
end)
else
ESX.ShowNotification("Du äger ~r~inget~s~ fordon.")
return
end
end)
end
CheckRaceMembers = function()
local elements = {}
ESX.TriggerServerCallback("unrp_streetrace:fetchMembers", function(memberArray)
if not memberArray then
ESX.ShowNotification("Inga är ~r~anmälda.")
return
end
local winAmount = 0
for src, vals in pairs(memberArray) do
winAmount = winAmount + vals["wager"]
table.insert(elements, { ["label"] = vals["name"] .. " - " .. vals["wager"] .. " SEK" })
end
ESX.UI.Menu.Open('default', GetCurrentResourceName(), "streetrace_menu_members_joined",
{
title = "Anmälda Personer - Vinst: " .. winAmount .. " SEK",
align = "center",
elements = elements
},
function(data, menu)
end, function(data, menu)
menu.close()
end)
end)
end
backup.lua
local cachedLeaderboard = {}
RegisterNetEvent("unrp_streetrace:setPosition")
AddEventHandler("unrp_streetrace:setPosition", function(values, leaderboardPlayers)
cachedLeaderboard = leaderboardPlayers
local startPosition = Config_storage.StartPositions[values["position"]]
SetEntityCoords(PlayerPedId(), vector3(startPosition["x"], startPosition["y"], startPosition["z"]))
ESX.Game.SpawnVehicle(values["vehicle"]["model"], startPosition, startPosition["h"], function(vehicle)
ESX.Game.SetVehicleProperties(vehicle, values["vehicle"])
TriggerEvent("negerfuel:setEssence", 100, GetVehicleNumberPlateText(vehicle), GetDisplayNameFromVehicleModel(GetEntityModel(vehicle)))
TaskWarpPedIntoVehicle(PlayerPedId(), vehicle, -1)
SetEntityAsMissionEntity(vehicle, true, true)
end)
end)
RegisterNetEvent("unrp_streetrace:start")
AddEventHandler("unrp_streetrace:start", function()
if hasJoinedRace then
StartCountdown()
end
end)
StartCountdown = function()
local countDownTimer = 4
FreezeEntityPosition(GetVehiclePedIsUsing(PlayerPedId()), true)
for i = 1, countDownTimer, 1 do
countDownTimer = countDownTimer - 1
ESX.ShowNotification(tostring(countDownTimer))
Citizen.Wait(1000)
end
FreezeEntityPosition(GetVehiclePedIsUsing(PlayerPedId()), false)
StartRace()
end
StartRace = function()
local currentCheckPoint = 1
local nextCheckPoint = 2
local isRacing = true
StartTime = GetGameTimer()
Citizen.CreateThread(function()
CheckPoint = CreateCheckpoint(5, Config_storage.StreetRace[currentCheckPoint]["x"], Config_storage.StreetRace[currentCheckPoint]["y"], Config_storage.StreetRace[currentCheckPoint]["z"] + 2, Config_storage.StreetRace[nextCheckPoint]["x"], Config_storage.StreetRace[nextCheckPoint]["y"], Config_storage.StreetRace[nextCheckPoint]["z"], 8.0, 255, 255, 255, 100, 0)
Blip = AddBlipForCoord(Config_storage.StreetRace[currentCheckPoint]["x"], Config_storage.StreetRace[currentCheckPoint]["y"], Config_storage.StreetRace[currentCheckPoint]["z"])
while isRacing do
Citizen.Wait(5)
local PlayerCoords = GetEntityCoords(PlayerPedId())
local currentTime = formatTimer(StartTime, GetGameTimer())
if not IsPedInAnyVehicle(PlayerPedId(), false) then
ESX.ShowNotification("Du lämnade fordonet vilket orsakade diskvalificering!")
DeleteCheckpoint(CheckPoint)
RemoveBlip(Blip)
isRacing = false
return
end
ESX.Game.Utils.DrawText3D(PlayerCoords, currentTime, 0.4)
if GetDistanceBetweenCoords(GetEntityCoords(PlayerPedId()), Config_storage.StreetRace[currentCheckPoint]["x"], Config_storage.StreetRace[currentCheckPoint]["y"], Config_storage.StreetRace[currentCheckPoint]["z"]) < 7.5 then
if currentCheckPoint == #Config_storage.StreetRace - 1 then
currentCheckPoint = #Config_storage.StreetRace
PlaySoundFrontend(-1, "RACE_PLACED", "HUD_AWARDS")
DeleteCheckpoint(CheckPoint)
RemoveBlip(Blip)
CheckPoint = CreateCheckpoint(9, Config_storage.StreetRace[currentCheckPoint]["x"], Config_storage.StreetRace[currentCheckPoint]["y"], Config_storage.StreetRace[currentCheckPoint]["z"] + 2, Config_storage.StreetRace[nextCheckPoint]["x"], Config_storage.StreetRace[nextCheckPoint]["y"], Config_storage.StreetRace[nextCheckPoint]["z"], 8.0, 255, 255, 255, 100, 0)
elseif currentCheckPoint == #Config_storage.StreetRace then
PlaySoundFrontend(-1, "ScreenFlash", "WastedSounds")
DeleteCheckpoint(CheckPoint)
RemoveBlip(Blip)
ESX.ShowNotification("Du körde klart racet med en tid på " .. currentTime .. " sekunder!")
DeleteEntity(RaceVehicle)
return
else
PlaySoundFrontend(-1, "RACE_PLACED", "HUD_AWARDS")
DeleteCheckpoint(CheckPoint)
RemoveBlip(Blip)
currentCheckPoint = currentCheckPoint + 1
nextCheckPoint = nextCheckPoint + 1
CheckPoint = CreateCheckpoint(5, Config_storage.StreetRace[currentCheckPoint]["x"], Config_storage.StreetRace[currentCheckPoint]["y"], Config_storage.StreetRace[currentCheckPoint]["z"] + 2, Config_storage.StreetRace[nextCheckPoint]["x"], Config_storage.StreetRace[nextCheckPoint]["y"], Config_storage.StreetRace[nextCheckPoint]["z"], 8.0, 255, 255, 255, 100, 0)
Blip = AddBlipForCoord(Config_storage.StreetRace[currentCheckPoint]["x"], Config_storage.StreetRace[currentCheckPoint]["y"], Config_storage.StreetRace[currentCheckPoint]["z"])
end
end
end
end)
end
formatTimer = function(startTime, currTime)
local newString = currTime - startTime
local ms = string.sub(newString, -3, -2)
local sec = string.sub(newString, -5, -4)
local min = string.sub(newString, -7, -6)
newString = string.format("%s%s.%s", min, sec, ms)
return newString
end
main.lua
hasJoinedRace = false
RegisterNetEvent("unrp_streetrace:setWaypoint")
AddEventHandler("unrp_streetrace:setWaypoint", function()
local streetRaceMenu = Config_storage.StreetraceMenu
SetNewWaypoint(streetRaceMenu["x"], streetRaceMenu["y"])
end)
Citizen.CreateThread(function()
Citizen.Wait(100)
while true do
local sleepThread = 500
local ped = PlayerPedId()
local pedCoords = GetEntityCoords(ped)
local streetRaceMenu = Config_storage.StreetraceMenu
local dstCheck = GetDistanceBetweenCoords(pedCoords, streetRaceMenu["x"], streetRaceMenu["y"], streetRaceMenu["z"], true)
if dstCheck <= 3.0 then
sleepThread = 5
text = "Streetrace"
if dstCheck <= 0.8 then
text = "[~g~E~s~] Streetrace"
if IsControlJustPressed(0, 38) then
OpenStreetRaceMenu()
end
end
ESX.DrawMarker(text, 23, streetRaceMenu["x"], streetRaceMenu["y"], streetRaceMenu["z"] - 0.985, 0, 255, 0, 1.0, 1.0)
end
Citizen.Wait(sleepThread)
end
end)
streetrace.lua
RegisterNetEvent("unrp_streetrace:setPosition")
AddEventHandler("unrp_streetrace:setPosition", function(values)
DoScreenFadeOut(1)
local startPosition = Config_storage.StartPositions[values["position"]]
SetEntityCoords(PlayerPedId(), vector3(startPosition["x"], startPosition["y"], startPosition["z"]))
ESX.Game.SpawnVehicle(values["vehicle"]["model"], startPosition, startPosition["h"], function(vehicle)
ESX.Game.SetVehicleProperties(vehicle, values["vehicle"])
TriggerEvent("negerfuel:setEssence", 100, GetVehicleNumberPlateText(vehicle), GetDisplayNameFromVehicleModel(GetEntityModel(vehicle)))
TaskWarpPedIntoVehicle(PlayerPedId(), vehicle, -1)
SetEntityAsMissionEntity(vehicle, true, true)
end)
StartCountdown()
end)
StartCountdown = function()
Citizen.Wait(500)
DoScreenFadeIn(1)
local countDownTimer = Config_storage.CountdownTimer + 1
FreezeEntityPosition(GetVehiclePedIsUsing(PlayerPedId()), true)
for i = 1, countDownTimer, 1 do
countDownTimer = countDownTimer - 1
ESX.ShowNotification(tostring(countDownTimer), "", 600)
Citizen.Wait(1000)
end
FreezeEntityPosition(GetVehiclePedIsUsing(PlayerPedId()), false)
StartRace()
end
StartRace = function()
local currentCheckPoint = 1
local nextCheckPoint = 2
local isRacing = true
StartTime = GetGameTimer()
Citizen.CreateThread(function()
CheckPoint = CreateCheckpoint(5, Config_storage.StreetRace[currentCheckPoint]["x"], Config_storage.StreetRace[currentCheckPoint]["y"], Config_storage.StreetRace[currentCheckPoint]["z"] + 2, Config_storage.StreetRace[nextCheckPoint]["x"], Config_storage.StreetRace[nextCheckPoint]["y"], Config_storage.StreetRace[nextCheckPoint]["z"], 8.0, 255, 255, 255, 100, 0)
Blip = AddBlipForCoord(Config_storage.StreetRace[currentCheckPoint]["x"], Config_storage.StreetRace[currentCheckPoint]["y"], Config_storage.StreetRace[currentCheckPoint]["z"])
while isRacing do
Citizen.Wait(5)
local PlayerCoords = GetEntityCoords(PlayerPedId())
if not IsPedInAnyVehicle(PlayerPedId(), false) then
ESX.ShowNotification("Du lämnade fordonet vilket orsakade diskvalificering!")
DeleteCheckpoint(CheckPoint)
RemoveBlip(Blip)
TriggerServerEvent("unrp_streetrace:playerFinished", "DNF")
isRacing = false
return
end
if GetDistanceBetweenCoords(GetEntityCoords(PlayerPedId()), Config_storage.StreetRace[currentCheckPoint]["x"], Config_storage.StreetRace[currentCheckPoint]["y"], Config_storage.StreetRace[currentCheckPoint]["z"]) < 7.5 then
if currentCheckPoint == #Config_storage.StreetRace - 1 then
currentCheckPoint = #Config_storage.StreetRace
PlaySoundFrontend(-1, "RACE_PLACED", "HUD_AWARDS")
DeleteCheckpoint(CheckPoint)
RemoveBlip(Blip)
CheckPoint = CreateCheckpoint(9, Config_storage.StreetRace[currentCheckPoint]["x"], Config_storage.StreetRace[currentCheckPoint]["y"], Config_storage.StreetRace[currentCheckPoint]["z"] + 2, Config_storage.StreetRace[nextCheckPoint]["x"], Config_storage.StreetRace[nextCheckPoint]["y"], Config_storage.StreetRace[nextCheckPoint]["z"], 8.0, 255, 255, 255, 100, 0)
elseif currentCheckPoint == #Config_storage.StreetRace then
PlaySoundFrontend(-1, "ScreenFlash", "WastedSounds")
DeleteCheckpoint(CheckPoint)
RemoveBlip(Blip)
TriggerServerEvent("unrp_streetrace:playerFinished")
hasJoinedRace = false
return
else
PlaySoundFrontend(-1, "RACE_PLACED", "HUD_AWARDS")
DeleteCheckpoint(CheckPoint)
RemoveBlip(Blip)
currentCheckPoint = currentCheckPoint + 1
nextCheckPoint = nextCheckPoint + 1
CheckPoint = CreateCheckpoint(5, Config_storage.StreetRace[currentCheckPoint]["x"], Config_storage.StreetRace[currentCheckPoint]["y"], Config_storage.StreetRace[currentCheckPoint]["z"] + 2, Config_storage.StreetRace[nextCheckPoint]["x"], Config_storage.StreetRace[nextCheckPoint]["y"], Config_storage.StreetRace[nextCheckPoint]["z"], 8.0, 255, 255, 255, 100, 0)
Blip = AddBlipForCoord(Config_storage.StreetRace[currentCheckPoint]["x"], Config_storage.StreetRace[currentCheckPoint]["y"], Config_storage.StreetRace[currentCheckPoint]["z"])
SetBlipRoute(Blip, true)
end
end
end
hasJoinedRace = false
end)
end
config.lua
Config_storage = {}
Config_storage.CountdownTimer = 10
Config_storage.StreetraceMenu = { ["x"] = -36.31, ["y"] = -2095.64, ["z"] = 16.94 }
Config_storage.StartPositions = {
{ ["x"] = -50.75, ["y"] = -2114.88, ["z"] = 16.7, ["h"] = 107.46 },
{ ["x"] = -52.44, ["y"] = -2110.62, ["z"] = 16.7, ["h"] = 109.97 },
{ ["x"] = -44.78, ["y"] = -2107.89, ["z"] = 16.7, ["h"] = 109.79 },
{ ["x"] = -43.13, ["y"] = -2112.42, ["z"] = 16.7, ["h"] = 109.96 },
{ ["x"] = -35.84, ["y"] = -2109.82, ["z"] = 16.7, ["h"] = 110.13 },
{ ["x"] = -37.55, ["y"] = -2105.27, ["z"] = 16.7, ["h"] = 111.36 }
}
Config_storage.StreetRace = {
{ ["x"] = -155.92, ["y"] = -2149.31, ["z"] = 16.35 },
{ ["x"] = -284.57, ["y"] = -2177.15, ["z"] = 9.98 },
{ ["x"] = -442.61, ["y"] = -2155.48, ["z"] = 9.96 },
{ ["x"] = -502.59, ["y"] = -2139.56, ["z"] = 8.7 },
{ ["x"] = -644.53, ["y"] = -2304.36, ["z"] = 6.37 },
{ ["x"] = -800.59, ["y"] = -2486.1, ["z"] = 13.28 },
{ ["x"] = -803.84, ["y"] = -2240.72, ["z"] = 16.96 },
{ ["x"] = -700.45, ["y"] = -2128.76, ["z"] = 12.89 },
{ ["x"] = -390.1, ["y"] = -1841.22, ["z"] = 21.09 },
{ ["x"] = -383.73, ["y"] = -1711.22, ["z"] = 18.42 },
{ ["x"] = -268.05, ["y"] = -1455.18, ["z"] = 30.96 },
{ ["x"] = -120.71, ["y"] = -1375.62, ["z"] = 29.07 },
{ ["x"] = -94.25, ["y"] = -1149.3, ["z"] = 25.45 },
{ ["x"] = 47.14, ["y"] = -1138.79, ["z"] = 28.98 },
{ ["x"] = 81.73, ["y"] = -1336.6, ["z"] = 28.99 },
{ ["x"] = 146.65, ["y"] = -1392.28, ["z"] = 28.96 },
{ ["x"] = 63.9, ["y"] = -1502.22, ["z"] = 28.99 },
{ ["x"] = 2.63, ["y"] = -1461.47, ["z"] = 30.23 },
{ ["x"] = -103.42, ["y"] = -1507.63, ["z"] = 33.39 },
{ ["x"] = -180.0, ["y"] = -1597.4, ["z"] = 34.03 },
{ ["x"] = -147.03, ["y"] = -1720.1, ["z"] = 29.78 },
{ ["x"] = -85.02, ["y"] = -1731.5, ["z"] = 28.99 },
{ ["x"] = 45.32, ["y"] = -1682.91, ["z"] = 28.92 },
{ ["x"] = 159.24, ["y"] = -1775.92, ["z"] = 28.66 },
{ ["x"] = 232.86, ["y"] = -1724.1, ["z"] = 28.72 },
{ ["x"] = 273.66, ["y"] = -1674.54, ["z"] = 28.99 },
{ ["x"] = 209.66, ["y"] = -1595.03, ["z"] = 28.85 },
{ ["x"] = 327.33, ["y"] = -1519.82, ["z"] = 28.95 },
{ ["x"] = 452.18, ["y"] = -1418.73, ["z"] = 28.99 },
{ ["x"] = 324.63, ["y"] = -1308.84, ["z"] = 31.56 },
{ ["x"] = 229.45, ["y"] = -1290.38, ["z"] = 28.93 },
{ ["x"] = 218.35, ["y"] = -1129.87, ["z"] = 28.94 },
{ ["x"] = 231.1, ["y"] = -1039.15, ["z"] = 29.02 },
{ ["x"] = 277.0, ["y"] = -856.97, ["z"] = 28.91 },
{ ["x"] = 181.66, ["y"] = -821.33, ["z"] = 30.82 },
{ ["x"] = 103.27, ["y"] = -987.22, ["z"] = 29.06 },
{ ["x"] = -32.35, ["y"] = -950.92, ["z"] = 29.06 },
{ ["x"] = -97.25, ["y"] = -1129.73, ["z"] = 25.45 },
{ ["x"] = -275.81, ["y"] = -1136.03, ["z"] = 22.74 },
{ ["x"] = -524.47, ["y"] = -1091.84, ["z"] = 22.19 },
{ ["x"] = -623.84, ["y"] = -1282.78, ["z"] = 10.33 },
{ ["x"] = -659.69, ["y"] = -1487.33, ["z"] = 10.35 },
{ ["x"] = -703.81, ["y"] = -1610.66, ["z"] = 22.24 },
{ ["x"] = -508.14, ["y"] = -1776.81, ["z"] = 20.86 },
{ ["x"] = -416.74, ["y"] = -1767.87, ["z"] = 20.29 },
{ ["x"] = -389.23, ["y"] = -1824.53, ["z"] = 21.18 },
{ ["x"] = -251.86, ["y"] = -1835.49, ["z"] = 28.44 },
{ ["x"] = -153.96, ["y"] = -2007.68, ["z"] = 22.53 },
{ ["x"] = -116.27, ["y"] = -2018.95, ["z"] = 17.67 },
{ ["x"] = -89.69, ["y"] = -2109.69, ["z"] = 16.35 }
}
Kolla på koden och gör en server.lua