OneSync Vehicle Spawning
What happened?
Hey guys.. im trying to develope some vehicleshop script, where the car will load after the player reach the radius.. i tried firstly to do this per client, bot got the issue the client tried one time to spawn them, if they are far away (300+) they dont spawn.. so now i tried to get this per server script and configuration, and tried to load them client-side if the player is 35 coords away. client-script: local spawnedCars = {}
-- Eventhandler zum Laden von Autos RegisterNetEvent("spawnCar") AddEventHandler("spawnCar", function(carConfig, plate, player) print("Client: spawnCar Event empfangen für Modell " .. carConfig.model .. " und Nummernschild " .. plate .. " von Spieler " .. player) local playerPed = GetPlayerPed(-1) local pos = GetEntityCoords(playerPed) local distance = GetDistanceBetweenCoords(pos.x, pos.y, pos.z, carConfig.x, carConfig.y, carConfig.z, true)
if distance <= 35.0 then
RequestModel(carConfig.model)
while not HasModelLoaded(carConfig.model) do
Wait(500)
end
local vehicle = CreateVehicle(carConfig.model, carConfig.x, carConfig.y, carConfig.z, carConfig.heading, true, false)
SetVehicleNumberPlateText(vehicle, plate)
table.insert(spawnedCars, vehicle)
end
end)
-- Eventhandler für das Betreten des Radius Citizen.CreateThread(function() while true do local playerPed = GetPlayerPed(-1) local pos = GetEntityCoords(playerPed)
for _, vehicle in pairs(spawnedCars) do
local vehiclePos = GetEntityCoords(vehicle)
local distance = GetDistanceBetweenCoords(pos.x, pos.y, pos.z, vehiclePos.x, vehiclePos.y, vehiclePos.z, true)
if distance > 35.0 then
DeleteVehicle(vehicle)
end
end
Wait(1000)
end
end) server-script: -- server.lua
-- Definiere die Liste der konfigurierbaren Autos in der Konfigurationsdatei (config.lua) local carConfigurations = {} local configFile = "config.lua"
-- Lade Konfigurationsdatei function loadConfigurations() local file = LoadResourceFile(GetCurrentResourceName(), configFile) if file then carConfigurations = load(file)() else print("Fehler beim Laden der Konfigurationsdatei.") end end
-- Eventhandler zum Spawnen von Autos RegisterNetEvent("spawnCar") AddEventHandler("spawnCar", function(carConfig, plate, player) print("Server: spawnCar Event ausgelöst für Modell " .. model .. " und Nummernschild " .. plate) local player = source
if carConfigurations[model] then
local carConfig = carConfigurations[model]
TriggerClientEvent("spawnCar", -1, carConfig, plate, player)
else
print("Ungültiges Auto-Modell: " .. model)
end
end)
-- Initialisierung AddEventHandler("onResourceStart", function(resourceName) if resourceName == GetCurrentResourceName() then loadConfigurations() end end) config: return { { model = "adder", x = 200.0, y = -1000.0, z = 30.0, heading = 180.0 }, { model = "comet2", x = 150.0, y = -950.0, z = 30.0, heading = 90.0 }, -- Weitere Autos können hier hinzugefügt werden }
like you see, i tried to build a log, also this is nonewhere shown... so it felt like the event got not triggered.. where is my thinking error guys?
Expected result
cars should load/spawn after player is <35 away
Reproduction steps
- Use my Script.
- ensure in server.cfg
- get no scripting errors - no logs
- dont have some result anymore :)
Importancy
Unknown
Area(s)
FiveM, FXServer, OneSync
Specific version(s)
FXServer 7290
Additional information
No response
For help with scripting related problems try the Cfx forum or Discord guild (Channel -> #fivem-scripting)
The Github repo is not the right place to ask for assistance and should only be used for actual bugs/improvements, in general: workable stuff🙂