Jump to content

Vehicle Spawners


xXMADEXx

Recommended Posts

Posted

I've made this car spawner system, and its all workin good, exept for one thing, that i have no idea why its doing it. For some reason, the cars from the last created spawner are creating in ever vehicel spawner...

Server

marker = {} 
vehicleCarSpawns = { 
-- Format: x, y, z, rotation, "Group", {cars} 
{1476.7, -2776.3, 14.7, -90, "FBI", {596, 597, 598, 599, 579, 490, 528}}, 
{1523, -2847.8, 0, 180, "FBI", {472, 473, 595}} 
} 
  
for i,v in ipairs (vehicleCarSpawns) do 
    x,y,z,rot,group,cars = unpack(vehicleCarSpawns[i]) 
    local r,g,b = unpack(exports['[ROG]Groups']:getGangColor(group)) 
    marker[i] = createMarker(x, y, z - 1, "cylinder", 2.3, r, g, b, 150) 
    setElementData(marker[i],"markerType",group) 
    addEventHandler("onMarkerHit",marker[i], 
        function (p) 
            if (getElementData(source,"markerType")==exports['[ROG]Groups']:getPlayerGang(p)) then 
                triggerClientEvent(p,"showGroupVehicleSpawners",p,cars,x,y,z) 
            else 
                triggerClientEvent(p,"message:centerText",p,"Your not in the "..getElementData(source,"markerType"), 5, 255, 0, 0) 
            end 
        end 
    ) 
end 
  
vehicle = {} 
function onDestroyCustomCars() 
    if isElement(vehicle[source]) then 
        destroyElement(vehicle[source]) 
        vehicle[source] = nil 
    end 
end 
addEventHandler("onPlayerQuit", root, onDestroyCustomCars) 
addEvent("onDestroyCustomCars",true) 
addEventHandler("onDestroyCustomCars",root,onDestroyCustomCars) 
  
function onCreateGroupVehicle(id,x,y,z) 
    if (id) then 
        if (isElement(vehicle[source])) then 
            destroyElement(vehicle[source]) 
        end  
        local r,g,b = unpack(exports['[ROG]Groups']:getGangColor(group)) 
        triggerEvent("spawners:destroyOldCars",source) 
        if (tonumber(x) ~= nil) then 
            vehicle[source] = createVehicle(id, x, y, z, 0, 0, rot) 
        else 
            local rx,ry,rz = getElementPosition(source) 
            vehicle[source] = createVehicle(id, rx, ry, rz, 0, 0, 0) 
        end 
        warpPedIntoVehicle(source,vehicle[source]) 
        setElementData(vehicle[source],"vehOwner","spawners",true) 
        setVehicleColor(vehicle[source],r,g,b) 
    end 
end 
addEvent("onCreateGroupVehicle",true) 
addEventHandler("onCreateGroupVehicle",root,onCreateGroupVehicle) 
  

Client

local rx,ry = guiGetScreenSize() 
button = {} 
window = {} 
gridlist = {} 
  
window['groups'] = guiCreateWindow((rx/2-343/2),(rx/2-316/2),343,316,"",false) 
guiSetAlpha(window['groups'],1) 
guiWindowSetSizable(window['groups'],false) 
  
button['groupsSpawn'] = guiCreateButton(11,267,160,40,"Spawn",false,window['groups']) 
button['groupsClose'] = guiCreateButton(173,267,161,40,"Cancel",false,window['groups']) 
  
guiSetVisible( window['groups'], false ) 
  
gridlist['groups'] = guiCreateGridList(9,22,324,243,false,window['groups']) 
guiGridListAddColumn(gridlist['groups'],"Vehicle",0.4) 
guiGridListAddColumn(gridlist['groups'],"ID",0.4) 
  
function showGroupVehicleSpawners(cars,x,y,z) 
    x = x 
    y = y 
    z = z 
    guiSetVisible(window['groups'],true) 
    showCursor(true) 
    for i,v in ipairs (cars) do 
        local name = getVehicleNameFromModel(v)                                 -- BUGGED LINE 
        row = guiGridListAddRow(gridlist['groups']) 
        guiGridListSetItemText(gridlist['groups'], row, 1, name, false, false)  -- BUGGED LINE 
        guiGridListSetItemData(gridlist['groups'], row, 1, v) 
        guiGridListSetItemText(gridlist['groups'], row, 2, tostring(v), false, false) 
        guiGridListSetItemData(gridlist['groups'], row, 2, v) 
    end 
end 
addEvent("showGroupVehicleSpawners",true) 
addEventHandler("showGroupVehicleSpawners",root,showGroupVehicleSpawners) 
  
addEventHandler("onClientGUIClick",root, 
    function () 
        if (source==button['groupsClose']) then 
            guiGridListClear(gridlist['groups']) 
            guiSetVisible(window['groups'],false) 
            showCursor(false,false) 
        elseif (source==button['groupsSpawn']) then 
            local row, col = guiGridListGetSelectedItem(gridlist['groups']) 
            local id = guiGridListGetItemData(gridlist['groups'], row, 1) 
            triggerServerEvent("onCreateGroupVehicle",localPlayer,id,x,y,z) 
            guiGridListClear(gridlist['groups']) 
            guiSetVisible(window['groups'],false) 
            showCursor(false,false) 
        end 
    end 
) 
  

Posted
Put it here under :

local name = getVehicleNameFromModel(v)

#

Well, i did that and it was a fail it still has problem, exept now it will only list the first last

Posted
Put it here under :

local name = getVehicleNameFromModel(v)

#

That would clear it a lot of times.

@MADE: Put it under:

for i,v in ipairs (cars) do 

Posted
Put it here under :

local name = getVehicleNameFromModel(v)

#

That would clear it a lot of times.

@MADE: Put it under:

for i,v in ipairs (cars) do 

It still happend, only the last car, for the last spawner will appear.

Posted
Did u remove line 47?

Yea. This is the code now:

local rx,ry = guiGetScreenSize() 
button = {} 
window = {} 
gridlist = {} 
  
window['groups'] = guiCreateWindow((rx/2-343/2),(rx/2-316/2),343,316,"",false) 
guiSetAlpha(window['groups'],1) 
guiWindowSetSizable(window['groups'],false) 
  
button['groupsSpawn'] = guiCreateButton(11,267,160,40,"Spawn",false,window['groups']) 
button['groupsClose'] = guiCreateButton(173,267,161,40,"Cancel",false,window['groups']) 
  
guiSetVisible( window['groups'], false ) 
  
gridlist['groups'] = guiCreateGridList(9,22,324,243,false,window['groups']) 
guiGridListAddColumn(gridlist['groups'],"Vehicle",0.4) 
guiGridListAddColumn(gridlist['groups'],"ID",0.4) 
  
function showGroupVehicleSpawners(cars,x,y,z) 
    x = x 
    y = y 
    z = z 
    guiSetVisible(window['groups'],true) 
    showCursor(true) 
    for i,v in ipairs (cars) do 
        guiGridListClear(gridlist['groups']) 
        local name = getVehicleNameFromModel(v) 
        row = guiGridListAddRow(gridlist['groups']) 
        guiGridListSetItemText(gridlist['groups'], row, 1, name, false, false) 
        guiGridListSetItemData(gridlist['groups'], row, 1, v) 
        guiGridListSetItemText(gridlist['groups'], row, 2, tostring(v), false, false) 
        guiGridListSetItemData(gridlist['groups'], row, 2, v) 
    end 
end 
addEvent("showGroupVehicleSpawners",true) 
addEventHandler("showGroupVehicleSpawners",root,showGroupVehicleSpawners) 
  
addEventHandler("onClientGUIClick",root, 
    function () 
        if (source==button['groupsClose']) then 
            guiGridListClear(gridlist['groups']) 
            guiSetVisible(window['groups'],false) 
            showCursor(false,false) 
        elseif (source==button['groupsSpawn']) then 
            local row, col = guiGridListGetSelectedItem(gridlist['groups']) 
            local id = guiGridListGetItemData(gridlist['groups'], row, 1) 
            triggerServerEvent("onCreateGroupVehicle",localPlayer,id,x,y,z) 
            guiSetVisible(window['groups'],false) 
            showCursor(false,false) 
        end 
    end 
) 
  

Posted
function showGroupVehicleSpawners(cars,x,y,z) 
    x = x 
    y = y 
    z = z 
    guiSetVisible(window['groups'],true) 
    showCursor(true) 
        guiGridListClear(gridlist['groups']) 
    for i,v in ipairs (cars) do 
        local name = getVehicleNameFromModel(v) 
        row = guiGridListAddRow(gridlist['groups']) 
        guiGridListSetItemText(gridlist['groups'], row, 1, name, false, false) 
        guiGridListSetItemData(gridlist['groups'], row, 1, v) 
        guiGridListSetItemText(gridlist['groups'], row, 2, tostring(v), false, false) 
        guiGridListSetItemData(gridlist['groups'], row, 2, v) 
    end 
end 
addEvent("showGroupVehicleSpawners",true) 
addEventHandler("showGroupVehicleSpawners",root,showGroupVehicleSpawners) 

Posted
function showGroupVehicleSpawners(cars,x,y,z) 
    x = x 
    y = y 
    z = z 
    guiSetVisible(window['groups'],true) 
    showCursor(true) 
        guiGridListClear(gridlist['groups']) 
    for i,v in ipairs (cars) do 
        local name = getVehicleNameFromModel(v) 
        row = guiGridListAddRow(gridlist['groups']) 
        guiGridListSetItemText(gridlist['groups'], row, 1, name, false, false) 
        guiGridListSetItemData(gridlist['groups'], row, 1, v) 
        guiGridListSetItemText(gridlist['groups'], row, 2, tostring(v), false, false) 
        guiGridListSetItemData(gridlist['groups'], row, 2, v) 
    end 
end 
addEvent("showGroupVehicleSpawners",true) 
addEventHandler("showGroupVehicleSpawners",root,showGroupVehicleSpawners) 

Still, it's still creating the vehicles from the last created spawner in the "vehicleCarSpawns" tabel, in all spawners. (i know i wasn't very clear right there.)

Posted

Maybe the problem is not with the GUI, maybe is with your table? tried using outputChatBox to see the results when looping the table to insert to the grid list?

Posted
marker = {} 
vehicleCarSpawns = { 
-- Format: x, y, z, rotation, "Group", {cars} 
{1476.7, -2776.3, 14.7, -90, "FBI", {596, 597, 598, 599, 579, 490, 528}}, 
{1523, -2847.8, 0, 180, "FBI", {472, 473, 595}} 
} 
  
addEventHandler ( "onResourceStart", resourceRoot, 
    function ( ) 
        for i,v in ipairs ( vehicleCarSpawns ) do 
            local x, y, z, rot, group, cars = unpack ( vehicleCarSpawns [ i ] ) 
            local r, g, b = unpack ( exports [ '[ROG]Groups' ]:getGangColor ( group ) ) 
            marker [ i ] = createMarker ( x, y, z - 1, "cylinder", 2.3, r, g, b, 150 ) 
            setElementData ( marker [ i ], "markerIndex", i, false ) 
            addEventHandler ( "onMarkerHit", marker [ i ], onSpawnerMarkerHit ) 
        end 
    end 
) 
  
function onSpawnerMarkerHit ( p ) 
    local index = getElementData ( source, "markerIndex" ) 
    local x, y, z, rot, group, cars = unpack ( vehicleCarSpawns [ index ] ) 
    if ( group == exports [ '[ROG]Groups' ]:getPlayerGang ( p ) ) then 
        triggerClientEvent ( p, "showGroupVehicleSpawners", p, cars, x, y, z ) 
    else 
        triggerClientEvent ( p, "message:centerText", p, "Your not in the ".. group, 5, 255, 0, 0 ) 
    end 
end 
  
vehicle = {} 
function onDestroyCustomCars() 
    if isElement(vehicle[source]) then 
        destroyElement(vehicle[source]) 
        vehicle[source] = nil 
    end 
end 
addEventHandler("onPlayerQuit", root, onDestroyCustomCars) 
addEvent("onDestroyCustomCars",true) 
addEventHandler("onDestroyCustomCars",root,onDestroyCustomCars) 
  
function onCreateGroupVehicle(id,x,y,z) 
    if (id) then 
        if (isElement(vehicle[source])) then 
            destroyElement(vehicle[source]) 
        end  
        local r,g,b = unpack(exports['[ROG]Groups']:getGangColor(group)) 
        triggerEvent("spawners:destroyOldCars",source) 
        if (tonumber(x) ~= nil) then 
            vehicle[source] = createVehicle(id, x, y, z, 0, 0, rot) 
        else 
            local rx,ry,rz = getElementPosition(source) 
            vehicle[source] = createVehicle(id, rx, ry, rz, 0, 0, 0) 
        end 
        warpPedIntoVehicle(source,vehicle[source]) 
        setElementData(vehicle[source],"vehOwner","spawners",true) 
        setVehicleColor(vehicle[source],r,g,b) 
    end 
end 
addEvent("onCreateGroupVehicle",true) 
addEventHandler("onCreateGroupVehicle",root,onCreateGroupVehicle) 

Posted
marker = {} 
vehicleCarSpawns = { 
-- Format: x, y, z, rotation, "Group", {cars} 
{1476.7, -2776.3, 14.7, -90, "FBI", {596, 597, 598, 599, 579, 490, 528}}, 
{1523, -2847.8, 0, 180, "FBI", {472, 473, 595}} 
} 
  
addEventHandler ( "onResourceStart", resourceRoot, 
    function ( ) 
        for i,v in ipairs ( vehicleCarSpawns ) do 
            local x, y, z, rot, group, cars = unpack ( vehicleCarSpawns [ i ] ) 
            local r, g, b = unpack ( exports [ '[ROG]Groups' ]:getGangColor ( group ) ) 
            marker [ i ] = createMarker ( x, y, z - 1, "cylinder", 2.3, r, g, b, 150 ) 
            setElementData ( marker [ i ], "markerIndex", i, false ) 
            addEventHandler ( "onMarkerHit", marker [ i ], onSpawnerMarkerHit ) 
        end 
    end 
) 
  
function onSpawnerMarkerHit ( p ) 
    local index = getElementData ( source, "markerIndex" ) 
    local x, y, z, rot, group, cars = unpack ( vehicleCarSpawns [ index ] ) 
    if ( group == exports [ '[ROG]Groups' ]:getPlayerGang ( p ) ) then 
        triggerClientEvent ( p, "showGroupVehicleSpawners", p, cars, x, y, z ) 
    else 
        triggerClientEvent ( p, "message:centerText", p, "Your not in the ".. group, 5, 255, 0, 0 ) 
    end 
end 
  
vehicle = {} 
function onDestroyCustomCars() 
    if isElement(vehicle[source]) then 
        destroyElement(vehicle[source]) 
        vehicle[source] = nil 
    end 
end 
addEventHandler("onPlayerQuit", root, onDestroyCustomCars) 
addEvent("onDestroyCustomCars",true) 
addEventHandler("onDestroyCustomCars",root,onDestroyCustomCars) 
  
function onCreateGroupVehicle(id,x,y,z) 
    if (id) then 
        if (isElement(vehicle[source])) then 
            destroyElement(vehicle[source]) 
        end  
        local r,g,b = unpack(exports['[ROG]Groups']:getGangColor(group)) 
        triggerEvent("spawners:destroyOldCars",source) 
        if (tonumber(x) ~= nil) then 
            vehicle[source] = createVehicle(id, x, y, z, 0, 0, rot) 
        else 
            local rx,ry,rz = getElementPosition(source) 
            vehicle[source] = createVehicle(id, rx, ry, rz, 0, 0, 0) 
        end 
        warpPedIntoVehicle(source,vehicle[source]) 
        setElementData(vehicle[source],"vehOwner","spawners",true) 
        setVehicleColor(vehicle[source],r,g,b) 
    end 
end 
addEvent("onCreateGroupVehicle",true) 
addEventHandler("onCreateGroupVehicle",root,onCreateGroupVehicle) 

Thank you castillo, code works :)

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...