1
0
Fork 0
forked from Simnation/Main
Main/resources/[standalone]/start_train/server.lua

140 lines
5.5 KiB
Lua
Raw Normal View History

2025-08-13 16:59:56 +02:00
local QBCore = exports['qb-core']:GetCoreObject()
-- Definiere die Orte wo das Event gestartet werden kann
local triggerLocations = {
{x = -1037.8, y = -2737.9, z = 20.2, name = "Los Santos Airport"},
{x = 215.3, y = -810.1, z = 30.7, name = "Legion Square"},
{x = -265.0, y = -957.3, z = 31.2, name = "Pillbox Hospital"},
{x = 425.1, y = -979.5, z = 30.7, name = "LSPD"},
{x = -1100.0, y = 2711.0, z = 19.0, name = "Sandy Shores Airfield"},
{x = 1747.0, y = 3273.0, z = 41.0, name = "Sandy Shores"},
-- Füge hier weitere Orte hinzu
}
-- Command um das Event an der nächsten Location zu triggern
QBCore.Commands.Add('starttrain', 'Starte Train Scenario', {}, false, function(source, args)
local Player = QBCore.Functions.GetPlayer(source)
if not Player then return end
local playerPed = GetPlayerPed(source)
local playerCoords = GetEntityCoords(playerPed)
-- Finde die nächste Location
local closestLocation = nil
local closestDistance = math.huge
for i, location in ipairs(triggerLocations) do
local distance = #(playerCoords - vector3(location.x, location.y, location.z))
if distance < closestDistance then
closestDistance = distance
closestLocation = location
end
end
-- Prüfe ob der Spieler nah genug an einer Location ist (50 Meter Radius)
if closestDistance <= 50.0 then
TriggerClientEvent('train:startscenario', source, 'Welcome')
TriggerClientEvent('QBCore:Notify', source, 'Train scenario gestartet bei: ' .. closestLocation.name, 'success', 5000)
else
TriggerClientEvent('QBCore:Notify', source, 'Du bist zu weit von einer Train-Location entfernt!', 'error', 5000)
end
end)
-- Command um eine neue Location hinzuzufügen (nur für Admins)
QBCore.Commands.Add('addtrainloc', 'Füge neue Train Location hinzu', {{name = 'name', help = 'Name der Location (optional)'}}, false, function(source, args)
local Player = QBCore.Functions.GetPlayer(source)
if not Player then return end
-- Prüfe Admin Berechtigung
if not QBCore.Functions.HasPermission(source, 'admin') then
TriggerClientEvent('QBCore:Notify', source, 'Du hast keine Berechtigung für diesen Command!', 'error')
return
end
local playerPed = GetPlayerPed(source)
local playerCoords = GetEntityCoords(playerPed)
local locationName = table.concat(args, " ")
if locationName == "" then
locationName = "Custom Location " .. (#triggerLocations + 1)
end
table.insert(triggerLocations, {
x = playerCoords.x,
y = playerCoords.y,
z = playerCoords.z,
name = locationName
})
TriggerClientEvent('QBCore:Notify', source, 'Neue Train-Location hinzugefügt: ' .. locationName, 'success')
TriggerClientEvent('train:updateLocations', -1, triggerLocations) -- Update für alle Clients
print("^2[TRAIN-TRIGGER]^7 Neue Location hinzugefügt: " .. locationName .. " bei " .. math.floor(playerCoords.x) .. ", " .. math.floor(playerCoords.y) .. ", " .. math.floor(playerCoords.z))
end, 'admin')
-- Command um alle Locations anzuzeigen
QBCore.Commands.Add('trainlocs', 'Zeige alle Train Locations', {}, false, function(source, args)
local Player = QBCore.Functions.GetPlayer(source)
if not Player then return end
TriggerClientEvent('QBCore:Notify', source, 'Schaue in die F8 Konsole für alle Locations', 'primary')
TriggerClientEvent('chat:addMessage', source, {
color = {255, 215, 0},
multiline = true,
args = {"^3[TRAIN]", "^7=== Train Locations ==="}
})
for i, location in ipairs(triggerLocations) do
TriggerClientEvent('chat:addMessage', source, {
color = {255, 255, 255},
multiline = true,
args = {"^3[TRAIN]", "^7" .. i .. ". ^2" .. location.name .. " ^7(" .. math.floor(location.x) .. ", " .. math.floor(location.y) .. ", " .. math.floor(location.z) .. ")"}
})
end
end)
-- Server Event für E-Taste Trigger
RegisterNetEvent('train:requestStart', function()
local source = source
local Player = QBCore.Functions.GetPlayer(source)
if not Player then return end
local playerPed = GetPlayerPed(source)
local playerCoords = GetEntityCoords(playerPed)
-- Finde die nächste Location
local closestLocation = nil
local closestDistance = math.huge
for i, location in ipairs(triggerLocations) do
local distance = #(playerCoords - vector3(location.x, location.y, location.z))
if distance < closestDistance then
closestDistance = distance
closestLocation = location
end
end
-- Prüfe ob der Spieler nah genug ist (10 Meter für E-Taste)
if closestDistance <= 10.0 then
TriggerClientEvent('train:startscenario', source, 'Welcome')
TriggerClientEvent('QBCore:Notify', source, 'Train scenario gestartet!', 'success')
end
end)
-- Event um Locations an Client zu senden
RegisterNetEvent('train:getLocations', function()
local source = source
TriggerClientEvent('train:receiveLocations', source, triggerLocations)
end)
-- Beim Server Start alle Locations loggen
AddEventHandler('onResourceStart', function(resourceName)
if GetCurrentResourceName() == resourceName then
print("^2[TRAIN-TRIGGER]^7 Resource gestartet mit " .. #triggerLocations .. " Locations")
for i, location in ipairs(triggerLocations) do
print("^3[TRAIN-TRIGGER]^7 " .. i .. ". " .. location.name)
end
end
end)