Jump to content

[Question] Adding multiple elements to table and destroying all elements from table data


kieran

Recommended Posts

I have made a trucker script, but have a few issues destroying markers, the idea is, I trigger a client side event to make multiple markers and add handlers to them, so when I hit the markers a GUI pops up, when I click accept, a marker and blip is created client side and a server side event is triggered to make a trailer, but I want to add all the markers to a table and destroy them when I click accept, but only one is destroyed, there are no errors on debug script 3, code below...

--x, y, z, location, cash
Locations = {
[1] = {-1351.5205078125, -503.66796875, 14.171875, "SF Airport"},
[2] = {2838.658203125, 999.6708984375, 10.75, "East LV"},
[3] = {2262.89453125, 2792.927734375, 10.8203125, "Near KACC"},
[4] = {2763.138671875, -2421.6845703125, 13.486039161682, "Test"},
[5] = {-576.53063964844, -550.34674072266, 25.529611587524, "Fallen Tree"}
}

Markers = {
[1] = {2762.677734375, -2431.4296875, 12.5},
[2] = {2747.6467285156, -2410.5747070313, 13.45193195343}
}

local GoodsMarker --Make a global variable
local markerVault = {} --

local Name
local marker
local blip
local cash

window = {}

function showGUI()
local screenW, screenH = guiGetScreenSize()
    window = guiCreateWindow((screenW - 498) / 2, (screenH - 266) / 2, 498, 266, "Shipments", false)
    guiWindowSetSizable(window, false)
    gridlist = guiCreateGridList(9, 27, 479, 194, false, window)
    colLocation = guiGridListAddColumn(gridlist, "Location", 0.33)
    colDistance = guiGridListAddColumn(gridlist, "Distance", 0.33)
    colCash = guiGridListAddColumn(gridlist, "Cash", 0.33)
	for i=1,#Locations do --Here is the information for gridlist
        local x,y,z,text = Locations[i][1],Locations[i][2],Locations[i][3],Locations[i][4] --x,y,z,name of the location
		local px,py,pz = getElementPosition(localPlayer) --Get players position
		local distance = getDistanceBetweenPoints3D(px,py,pz,x,y,z) --get the distance between player and each point
		cash = math.floor(distance*3) --distance x 3 is the cash the player will get, rounded it so it's an integer not a float
		local row = guiGridListAddRow ( gridlist,text, math.floor(distance), cash ) --add a row for each table index... rounded distance so it doesn't look horrible
    end
    btn_acc = guiCreateButton(72, 231, 140, 25, "Accept", false, window) --accept button
    guiSetFont(btn_acc, "sa-header")
    btn_exit = guiCreateButton(284, 231, 140, 25, "Exit", false, window) --exit button
    guiSetFont(btn_exit, "sa-header")    
	showCursor(true)
	
	local playerVehicle = getPedOccupiedVehicle ( localPlayer ) --Freeze player
	if (playerVehicle) then
		setElementFrozen ( playerVehicle, true )
	end
	
	addEventHandler("onClientGUIClick", btn_acc, Accept)
	addEventHandler("onClientGUIClick", btn_exit, Exit)
end

addEvent("TruckerStart", true) --triggered server side when a player hits a marker
addEventHandler("TruckerStart", getRootElement(), showGUI)


function Accept(button, state)
	if source == btn_acc and button == "left" and state == "up" then 
		Name = guiGridListGetItemText(gridlist,guiGridListGetSelectedItem(gridlist),1) --Get the name (text) from gridlist (Location[4]) of selected row
        local x, y, z
        for i=1,#Locations do --For all locations I check the selected name against the rows in the table
            if Name == Locations[i][4] then
                x, y, z = Locations[i][1], Locations[i][2], Locations[i][3]
            end
        end
		if isElement (marker) then --if the player has already made a marker I remove handler and destroy the marker
			removeEventHandler("onClientMarkerHit", marker, reward)
			destroyElement(marker)
			destroyElement(blip)
		end
        marker = createMarker( x,y,z-1,"cylinder", 3, 0, 100, 0, 100 ) --I create the marker and blip and add the handler
		blip = createBlipAttachedTo(marker, 51 ) --Type, Size, R, G, B
		addEventHandler("onClientMarkerHit", marker, reward)
		
		removeEventHandler("onClientGUIClick", btn_acc, Accept) --I remove the handler from this function
		destroyElement(window) --I destroy the window, hide the cursor and trigger an event to make a trailer
		showCursor(false) 
		triggerServerEvent("createTrailer", localPlayer)
		
		if isElement (GoodsMarker) then --AND HERE IS WHERE I HAVE AN ISSUE, I want to destroy the markers I made on the function at the bottom
			local marker_data = markerVault[source]
			if marker_data then
				for i=1, #marker_data do
					destroyElement(i)
				end
			end
		end
		
		local playerVehicle = getPedOccupiedVehicle ( localPlayer ) --Unfreeze player
		if (playerVehicle) then
			-- set the new freeze status
			setElementFrozen ( playerVehicle, false )
		end
	end
end

function Exit(button, state) --Exit button destroys window and creates marker
	if source == btn_exit and button == "left" and state == "up" then 
		removeEventHandler("onClientGUIClick", btn_acc, Accept)
		removeEventHandler("onClientGUIClick", btn_exit, Exit)
		destroyElement(window)
		showCursor(false)
		
		triggerEvent("shipmentMarker", localPlayer)
		
		local playerVehicle = getPedOccupiedVehicle ( localPlayer )
		if (playerVehicle) then
			setElementFrozen ( playerVehicle, false )
		end
	end
end


function reward() --Give the player reward and destroy marker
	for i=1,#Locations do
        if Name == Locations[i][4] then
			destroyElement(marker)
			destroyElement(blip)
			triggerServerEvent("truckerReward", localPlayer, cash)
			triggerEvent ("TruckerStart", localPlayer)
        end
    end
end


function makeGoodsMarker()  --For each line in table, I make a marker
	for i=1,#Markers do 
        local x,y,z = Markers[i][1],Markers[i][2],Markers[i][3]--x = key 1, y = key 2, z = key 3		
		GoodsMarker = createMarker( x,y,z,"cylinder", 5, 0, 200, 55, 255 )
		markerVault[i] = GoodsMarker
		TruckerJobBlip = createBlipAttachedTo ( GoodsMarker, 56 )
		addEventHandler("onClientMarkerHit", GoodsMarker, trailerSpawn)
	end
end
addEvent("shipmentMarker", true) --I add a custom event as I want to trigger it again when the player clicks exit on GUI
addEventHandler("shipmentMarker", getRootElement(), makeGoodsMarker)

Markers made on line 128, meant to be destroyed on line 82.

Thanks for any help, been struggling with this for a while.

Link to comment
Locations = {
	{-1351.5205078125,-503.66796875,14.171875,"SF Airport"}, -- Locations[1]
	{2838.658203125,999.6708984375,10.75,"East LV"}, -- Locations[2]
	{2262.89453125,2792.927734375,10.8203125,"Near KACC"}, -- Locations[3]
	{2763.138671875,-2421.6845703125,13.486039161682,"Test"}, -- Locations[4]
	{-576.53063964844,-550.34674072266,25.529611587524,"Fallen Tree"} -- Locations[5]
}

Markers = {
	{2762.677734375,-2431.4296875,12.5}, -- Markers[1]
	{2747.6467285156,-2410.5747070313,13.45193195343} -- Markers[2]
}

Use better format, you don't need those numbers to define things in the table. Tables have index numbers, which is what you're trying to use. Unless u move [5] where [1] should be u won't need those.

Link to comment

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...