|
| 1 | +local screenW, screenH = guiGetScreenSize() |
| 2 | + |
| 3 | +local windowW, windowH = 300, 200 |
| 4 | +local windowX, windowY = (screenW - windowW) / 2, (screenH - windowH) / 2 |
| 5 | +local window = guiCreateWindow(windowX, windowY, windowW, windowH, "Vehicle Spawner", false) |
| 6 | + |
| 7 | +local label = guiCreateLabel(20, 40, 260, 30, "Enter Vehicle ID:", false, window) |
| 8 | +guiLabelSetHorizontalAlign(label, "center") |
| 9 | + |
| 10 | +local input = guiCreateEdit(50, 70, 200, 30, "", false, window) |
| 11 | + |
| 12 | +local spawnButton = guiCreateButton(50, 120, 200, 40, "Spawn Vehicle", false, window) |
| 13 | + |
| 14 | +guiSetVisible(window, false) |
| 15 | + |
| 16 | +local function spawnVehicleByID(vehicleID) |
| 17 | + if not vehicleID then |
| 18 | + outputChatBox("Error: Please enter a valid number!", 255, 0, 0) |
| 19 | + return |
| 20 | + end |
| 21 | + |
| 22 | + local x, y, z = getElementPosition(localPlayer) |
| 23 | + local rot = getPedRotation(localPlayer) |
| 24 | + local offsetDistance = 5 |
| 25 | + local spawnX = x + offsetDistance * math.sin(math.rad(-rot)) |
| 26 | + local spawnY = y + offsetDistance * math.cos(math.rad(-rot)) |
| 27 | + |
| 28 | + triggerServerEvent("newmodels-test_vehicles:requestVehicleSpawn", resourceRoot, localPlayer, vehicleID, spawnX, spawnY, z, rot) |
| 29 | +end |
| 30 | + |
| 31 | +local function requestVehicleSpawn() |
| 32 | + local vehicleID = tonumber(guiGetText(input)) |
| 33 | + spawnVehicleByID(vehicleID) |
| 34 | + guiSetVisible(window, false) |
| 35 | + showCursor(false) |
| 36 | + guiSetText(input, "") |
| 37 | +end |
| 38 | + |
| 39 | +addEventHandler("onClientGUIClick", spawnButton, requestVehicleSpawn, false) |
| 40 | + |
| 41 | +addEvent("newmodels-test_vehicles:vehicleSpawnResponse", true) |
| 42 | +addEventHandler("newmodels-test_vehicles:vehicleSpawnResponse", localPlayer, function(success, message) |
| 43 | + if success then |
| 44 | + outputChatBox(message, 0, 255, 0) |
| 45 | + else |
| 46 | + outputChatBox(message, 255, 0, 0) |
| 47 | + end |
| 48 | +end) |
| 49 | + |
| 50 | +local function onInputEnter() |
| 51 | + if source == input then |
| 52 | + requestVehicleSpawn() |
| 53 | + end |
| 54 | +end |
| 55 | + |
| 56 | +addEventHandler("onClientGUIAccepted", input, onInputEnter) |
| 57 | + |
| 58 | +local function toggleSpawnerGUI() |
| 59 | + local visible = guiGetVisible(window) |
| 60 | + guiSetVisible(window, not visible) |
| 61 | + showCursor(not visible) |
| 62 | + |
| 63 | + if not visible then |
| 64 | + guiBringToFront(input) |
| 65 | + guiFocus(input) |
| 66 | + end |
| 67 | +end |
| 68 | + |
| 69 | +bindKey("F4", "down", toggleSpawnerGUI) |
| 70 | +addCommandHandler("vspawner", toggleSpawnerGUI, false) |
| 71 | +addCommandHandler("spawnveh", function(cmd, id) spawnVehicleByID(tonumber(id)) end, false) |
0 commit comments