Jump to content

I have a little problem with tables and markers


Dzsozi (h03)

Recommended Posts

Hello!

I would like to create a table for skin shop markers, and create a marker for each position I give in the table. The markers are being created, the camera positions are set, everything is good except one thing, I always get the positions of the last given section of the table.

local originalSkin
local availableSkins = 
    { 
        2, 
        7, 
        12, 
        13, 
        16, 
        19, 
        20, 
        21, 
        22, 
        23, 
        24, 
        25, 
        26, 
    }

local skinShops =
	{ -- markerX, markerY, markerZ, markerDimension, markerInterior, previewX, previewY, previewZ, previewRot, previewDimension, previewInterior, previewCamX, previewCamY, previewCamZ, previewCamLX, previewCamLY, previewCamLZ, exitPosX, exitPosY, exitposZ
  		--marker #1
		{1950.908203125, -2049.2995605469, 12.4, 0, 0, --shopMarkerPos, dimension, interior
		1976.33984375, -2048.2937011719, 12.884400367737, 80, 0, 0, --previewPos, rot, dimension, interior
		1963.5827636719, -2053.611328125, 15.560099601746, 1964.4354248047, -2053.1540527344, 15.307735443115, --previewCamPos
		1974.8217773438, -2056.7233886719, 14.861300468445, 60, 0, 0}, --exitPos, rot, dimension, interior
		
  		--marker #2
		{1953.2912597656, -2073.4045410156, 12.933400154114, 0, 0,
		1939.267578125, -2085.708984375, 12.905400276184, -90, 0, 0,
		1951.8502197266, -2083.51171875, 14.838299751282, 1950.8914794922, -2083.7155761719, 14.640121459961,
		1954.2136230469, -2086.5083007813, 12.888199806213, 60, 0, 0},
	}

local index = {} 

addEventHandler("onResourceStart", resourceRoot, function()
	for k, v in pairs(skinShops) do
		current = k
	end
	for i = 1, #skinShops do
		local currentShop = skinShops[i]
		
		if currentShop then
			local skinShopMarker = createMarker(currentShop[1], currentShop[2], currentShop[3], "cylinder", 1.5, 0, 180, 255, 75)
			setElementDimension(skinShopMarker, currentShop[4])
			setElementInterior(skinShopMarker, currentShop[5])
			addEventHandler("onMarkerHit", skinShopMarker, selectSkin)
		end
	end
end)

function selectSkin (player) 
    showCursor ( player, true ) 
    bindKey ( player, "backspace", "down", cancelBuy ) 
    bindKey ( player, "enter", "down", buySkin ) 
    bindKey ( player, "arrow_l", "down", changeSkin ) 
    bindKey ( player, "arrow_r", "down", changeSkin ) 
    fadeCamera ( player, true ) 
    index [player] = 1 
    setElementPosition ( player, skinShops[current][6], skinShops[current][7], skinShops[current][8] ) 
    setPedRotation ( player, skinShops[current][9] )
	setElementDimension (player, skinShops[current][10])
	setElementInterior (player, skinShops[current][11])
    setElementFrozen ( player, true )
    setCameraMatrix ( player, skinShops[current][12], skinShops[current][13], skinShops[current][14], skinShops[current][15], skinShops[current][16], skinShops[current][17] )
    originalSkin = getElementModel( player )
end 
  
function cancelBuy ( player ) 
    showCursor ( player, false ) 
    setElementPosition ( player, skinShops[current][18], skinShops[current][19], skinShops[current][20] ) 
    setPedRotation ( player, skinShops[current][21] )
	setElementDimension(player, skinShops[current][22])
	setElementInterior(player, skinShops[current][23])
    setElementFrozen ( player, false ) 
    setCameraTarget ( player, player )
	setElementModel ( player, originalSkin )
end 
  
function changeSkin ( player, key ) 
    if (key == "arrow_l") then 
        if (index [player] == 1) then 
            index [player] = #availableSkins 
        else 
            index [player] = (index[player] - 1) 
        end 
    else 
        if (index [player] == #availableSkins) then 
            index [player] = 1 
        else 
            index [player] = (index[player] + 1) 
        end 
    end
    setElementModel (player, availableSkins[index [player]]) 
    playSoundFrontEnd (player, 32)
end 
  
  
function buySkin ( player ) 
    showCursor ( player, false ) 
    playSoundFrontEnd ( player, 44 )
    unbindKey ( player, "backspace", "down", cancelBuy ) 
    unbindKey ( player, "enter", "down", buySkin ) 
    unbindKey ( player, "arrow_l", "down", changeSkin ) 
    unbindKey ( player, "arrow_r", "down", changeSkin ) 
    
    setElementPosition ( player, skinShops[current][18], skinShops[current][19], skinShops[current][20] ) 
    setPedRotation ( player, skinShops[current][21] )
	setElementDimension(player, skinShops[current][22])
	setElementInterior(player, skinShops[current][23])
    setElementFrozen ( player, false )
    setCameraTarget ( player, player )
	takePlayerMoney(player, 50)
end

Here's my script. So for example if I go into marker #1 the positions are the positions of marker #2. How can I fix it, so each markers have their own camera positions and other settings that I give?

Link to comment

The variable "current" is always the last item in the table, because that's the last element in the loop. You need a way to store the details in each marker. Simply use setElementData(marker, "markerID", idInTheTable) and then you can retrieve the information using the table index.

Edited by pa3ck
Link to comment

Did you mean something like this?

local originalSkin
local current = 1
local availableSkins = 
    { 
        2, 
        7, 
        12, 
        13, 
        16, 
        19, 
        20, 
        21, 
        22, 
        23, 
        24, 
        25, 
        26, 
    }

local skinShops =
	{ -- markerX, markerY, markerZ, markerDimension, markerInterior, previewX, previewY, previewZ, previewRot, previewDimension, previewInterior, previewCamX, previewCamY, previewCamZ, previewCamLX, previewCamLY, previewCamLZ, exitPosX, exitPosY, exitposZ, markerID
  		--marker #1
		{1950.908203125, -2049.2995605469, 12.4, 0, 0, --shopMarkerPos, dimension, interior
		1976.33984375, -2048.2937011719, 12.884400367737, 80, 0, 0, --previewPos, rot, dimension, interior
		1963.5827636719, -2053.611328125, 15.560099601746, 1964.4354248047, -2053.1540527344, 15.307735443115, --previewCamPos
		1974.8217773438, -2056.7233886719, 14.861300468445, 60, 0, 0, --exitPos, rot, dimension, interior
		1}, --markerID
		
  		--marker #2
		{1953.2912597656, -2073.4045410156, 12.933400154114, 0, 0,
		1939.267578125, -2085.708984375, 12.905400276184, -90, 0, 0,
		1951.8502197266, -2083.51171875, 14.838299751282, 1950.8914794922, -2083.7155761719, 14.640121459961,
		1954.2136230469, -2086.5083007813, 12.888199806213, 60, 0, 0,
		2},
	}

local index = {} 

addEventHandler("onResourceStart", resourceRoot, function()
	for i = 1, #skinShops do
		local currentShop = skinShops[i]
		
		if currentShop then
			skinShopMarker = createMarker(currentShop[1], currentShop[2], currentShop[3], "cylinder", 1.5, 0, 180, 255, 75)
			setElementData(skinShopMarker, "3dtext", "Ruhabolt")
			setElementData(skinShopMarker, "markerID-->skinShop", currentShop[24])
			setElementDimension(skinShopMarker, currentShop[4])
			setElementInterior(skinShopMarker, currentShop[5])
			addEventHandler("onMarkerHit", skinShopMarker, selectSkin)
		end
	end
end)

function selectSkin (player)
	local current = getElementData(skinShopMarker, "markerID-->skinShop")
    showCursor ( player, true ) 
    bindKey ( player, "backspace", "down", cancelBuy ) 
    bindKey ( player, "enter", "down", buySkin ) 
    bindKey ( player, "arrow_l", "down", changeSkin ) 
    bindKey ( player, "arrow_r", "down", changeSkin ) 
    fadeCamera ( player, true ) 
    index [player] = 1 
    setElementPosition ( player, skinShops[current][6], skinShops[current][7], skinShops[current][8] ) 
    setPedRotation ( player, skinShops[current][9] )
	setElementDimension (player, skinShops[current][10])
	setElementInterior (player, skinShops[current][11])
    setElementFrozen ( player, true )
    setCameraMatrix ( player, skinShops[current][12], skinShops[current][13], skinShops[current][14], skinShops[current][15], skinShops[current][16], skinShops[current][17] )
    originalSkin = getElementModel( player )
end 
  
function cancelBuy ( player )
	local current = getElementData(skinShopMarker, "markerID-->skinShop")
    showCursor ( player, false ) 
    setElementPosition ( player, skinShops[current][18], skinShops[current][19], skinShops[current][20] ) 
    setPedRotation ( player, skinShops[current][21] )
	setElementDimension(player, skinShops[current][22])
	setElementInterior(player, skinShops[current][23])
    setElementFrozen ( player, false ) 
    setCameraTarget ( player, player )
	setElementModel ( player, originalSkin )
end 
  
function changeSkin ( player, key ) 
    if (key == "arrow_l") then 
        if (index [player] == 1) then 
            index [player] = #availableSkins 
        else 
            index [player] = (index[player] - 1) 
        end 
    else 
        if (index [player] == #availableSkins) then 
            index [player] = 1 
        else 
            index [player] = (index[player] + 1) 
        end 
    end
    setElementModel (player, availableSkins[index [player]]) 
    playSoundFrontEnd (player, 32)
end 
  
  
function buySkin ( player )
	local current = getElementData(skinShopMarker, "markerID-->skinShop")
    showCursor ( player, false ) 
    playSoundFrontEnd ( player, 44 )
    unbindKey ( player, "backspace", "down", cancelBuy ) 
    unbindKey ( player, "enter", "down", buySkin ) 
    unbindKey ( player, "arrow_l", "down", changeSkin ) 
    unbindKey ( player, "arrow_r", "down", changeSkin ) 
    
    setElementPosition ( player, skinShops[current][18], skinShops[current][19], skinShops[current][20] ) 
    setPedRotation ( player, skinShops[current][21] )
	setElementDimension(player, skinShops[current][22])
	setElementInterior(player, skinShops[current][23])
    setElementFrozen ( player, false )
    setCameraTarget ( player, player )
	takePlayerMoney(player, 50)
end

 

 

Or this?

 

local current = 1
local originalSkin
local availableSkins = 
    { 
        2, 
        7, 
        12, 
        13, 
        16, 
        19, 
        20, 
        21, 
        22, 
        23, 
        24, 
        25, 
        26, 
    }

local skinShops =
	{ -- markerX, markerY, markerZ, markerDimension, markerInterior, previewX, previewY, previewZ, previewRot, previewDimension, previewInterior, previewCamX, previewCamY, previewCamZ, previewCamLX, previewCamLY, previewCamLZ, exitPosX, exitPosY, exitposZ, markerID
  		--marker #1
		{1950.908203125, -2049.2995605469, 12.4, 0, 0, --shopMarkerPos, dimension, interior
		1976.33984375, -2048.2937011719, 12.884400367737, 80, 0, 0, --previewPos, rot, dimension, interior
		1963.5827636719, -2053.611328125, 15.560099601746, 1964.4354248047, -2053.1540527344, 15.307735443115, --previewCamPos
		1974.8217773438, -2056.7233886719, 14.861300468445, 60, 0, 0, --exitPos, rot, dimension, interior
		1}, --markerID
		
  		--marker #2
		{1953.2912597656, -2073.4045410156, 12.933400154114, 0, 0,
		1939.267578125, -2085.708984375, 12.905400276184, -90, 0, 0,
		1951.8502197266, -2083.51171875, 14.838299751282, 1950.8914794922, -2083.7155761719, 14.640121459961,
		1954.2136230469, -2086.5083007813, 12.888199806213, 60, 0, 0,
		2},
	}

local index = {} 

addEventHandler("onResourceStart", resourceRoot, function()
	for i = 1, #skinShops do
		local currentShop = skinShops[i]
		
		if currentShop then
			local skinShopMarker = createMarker(currentShop[1], currentShop[2], currentShop[3], "cylinder", 1.5, 0, 180, 255, 75)
			setElementData(skinShopMarker, "3dtext", "Ruhabolt")
			setElementData(skinShopMarker, "markerID-->skinShop", currentShop[24])
			setElementDimension(skinShopMarker, currentShop[4])
			setElementInterior(skinShopMarker, currentShop[5])
			addEventHandler("onMarkerHit", skinShopMarker, selectSkin)
			current = getElementData(skinShopMarker, "markerID-->skinShop")
		end
	end
end)

function selectSkin (player)
    showCursor ( player, true ) 
    bindKey ( player, "backspace", "down", cancelBuy ) 
    bindKey ( player, "enter", "down", buySkin ) 
    bindKey ( player, "arrow_l", "down", changeSkin ) 
    bindKey ( player, "arrow_r", "down", changeSkin ) 
    fadeCamera ( player, true ) 
    index [player] = 1 
    setElementPosition ( player, skinShops[current][6], skinShops[current][7], skinShops[current][8] ) 
    setPedRotation ( player, skinShops[current][9] )
	setElementDimension (player, skinShops[current][10])
	setElementInterior (player, skinShops[current][11])
    setElementFrozen ( player, true )
    setCameraMatrix ( player, skinShops[current][12], skinShops[current][13], skinShops[current][14], skinShops[current][15], skinShops[current][16], skinShops[current][17] )
    originalSkin = getElementModel( player )
end 
  
function cancelBuy ( player ) 
    showCursor ( player, false ) 
    setElementPosition ( player, skinShops[current][18], skinShops[current][19], skinShops[current][20] ) 
    setPedRotation ( player, skinShops[current][21] )
	setElementDimension(player, skinShops[current][22])
	setElementInterior(player, skinShops[current][23])
    setElementFrozen ( player, false ) 
    setCameraTarget ( player, player )
	setElementModel ( player, originalSkin )
end 
  
function changeSkin ( player, key ) 
    if (key == "arrow_l") then 
        if (index [player] == 1) then 
            index [player] = #availableSkins 
        else 
            index [player] = (index[player] - 1) 
        end 
    else 
        if (index [player] == #availableSkins) then 
            index [player] = 1 
        else 
            index [player] = (index[player] + 1) 
        end 
    end
    setElementModel (player, availableSkins[index [player]]) 
    playSoundFrontEnd (player, 32)
end 
  
  
function buySkin ( player ) 
    showCursor ( player, false ) 
    playSoundFrontEnd ( player, 44 )
    unbindKey ( player, "backspace", "down", cancelBuy ) 
    unbindKey ( player, "enter", "down", buySkin ) 
    unbindKey ( player, "arrow_l", "down", changeSkin ) 
    unbindKey ( player, "arrow_r", "down", changeSkin ) 
    
    setElementPosition ( player, skinShops[current][18], skinShops[current][19], skinShops[current][20] ) 
    setPedRotation ( player, skinShops[current][21] )
	setElementDimension(player, skinShops[current][22])
	setElementInterior(player, skinShops[current][23])
    setElementFrozen ( player, false )
    setCameraTarget ( player, player )
	takePlayerMoney(player, 50)
end

 

(None of them work)

Edited by Dzsozi
Link to comment

Oh, I understand, I could fix it, thank you very much!

I would have one more question. How can I make types of "shops"? For example I have marker #1 and there would be skin id 1 only, and at marker #2 there would be skin id 2 only, just an example, but hope you understand me. I assume I would have to do something with the availableSkins table, define the marker ID there as well, or something like that, but I don't know how. Could you help me with that please?

Link to comment

Yea, like:

local skinShops =
	{ -- markerX, markerY, markerZ, markerDimension, markerInterior, previewX, previewY, previewZ, previewRot, previewDimension, previewInterior, previewCamX, previewCamY, previewCamZ, previewCamLX, previewCamLY, previewCamLZ, exitPosX, exitPosY, exitposZ, markerID
  		--marker #1
		{1950.908203125, -2049.2995605469, 12.4, 0, 0, --shopMarkerPos, dimension, interior
		1976.33984375, -2048.2937011719, 12.884400367737, 80, 0, 0, --previewPos, rot, dimension, interior
		1963.5827636719, -2053.611328125, 15.560099601746, 1964.4354248047, -2053.1540527344, 15.307735443115, --previewCamPos
		1974.8217773438, -2056.7233886719, 14.861300468445, 60, 0, 0, --exitPos, rot, dimension, interior
		1, skins = {2, 4, 6, 8} },

 

Link to comment

Alright, so I bumped into another issue. I transfered the marker creation to client side, so I can add dxDrawText on them, and etc, but now I have a little problem with triggering the server side function. I have the following in the client side script:

addEventHandler("onClientResourceStart", getResourceRootElement(getThisResource()), function()
	for k, v in pairs(skinShops) do
		local skinShopBlip = createBlip(v[1], v[2], v[3], 45, 2, 0, 180, 255, 255)
		local skinShopMarker = createMarker(v[1], v[2], v[3], "cylinder", 1.5, 0, 180, 255, 75)
		setElementData(skinShopMarker, "markerID-->skinShop", v[24])
		addEventHandler("onClientRender", root, function()
			exports.vice_util:dxDrawTextOnElement(skinShopMarker, "Ruhabolt", 1, 20, 255, 255, 255, 255, 2, "default-bold")
		end)
		setElementDimension(skinShopMarker, v[4])
		setElementInterior(skinShopMarker, v[5])
		addEventHandler("onClientMarkerHit", skinShopMarker, function(hitPlayer, matchingDimension)
			triggerServerEvent("selectSkin", hitPlayer, hitPlayer)
		end)
	end
end)

The following in a shared/global script (in the meta file it's type is set to shared):

availableSkins = 
    { 
        2, 
        7, 
        12, 
        13, 
        16, 
        19, 
        20, 
        21, 
        22, 
        23, 
        24, 
        25, 
        26, 
    }

skinShops =
	{ -- markerX, markerY, markerZ, markerDimension, markerInterior, previewX, previewY, previewZ, previewRot, previewDimension, previewInterior, previewCamX, previewCamY, previewCamZ, previewCamLX, previewCamLY, previewCamLZ, exitPosX, exitPosY, exitposZ, markerID
  		--marker #1
		{413.72509765625, 1039.0155029297, 24.25, 0, 0, --shopMarkerPos, dimension, interior
		418.73413085938, 1038.1110839844, 25.25609588623, 61.661361694336, 0, 0, --previewPos, rot, dimension, interior
		414.91680908203, 1039.669921875, 26.514999389648, 415.83294677734, 1039.3103027344, 26.337924957275, --previewCamPos
		414.23324584961, 1042.4398193359, 25.409734725952, 5.6778540611267, 0, 0, --exitPos, rot, dimension, interior
		1}, --markerID
		
  		--marker #2
		{1953.2912597656, -2073.4045410156, 12.933400154114, 0, 0,
		1939.267578125, -2085.708984375, 12.905400276184, -90, 0, 0,
		1951.8502197266, -2083.51171875, 14.838299751282, 1950.8914794922, -2083.7155761719, 14.640121459961,
		1954.2136230469, -2086.5083007813, 12.888199806213, 60, 0, 0,
		2},
	}

And the following in the server side script:

local originalSkin
local index = {} 

--[[addEventHandler("onResourceStart", resourceRoot, function()
	for i = 1, #skinShops do
		local currentShop = skinShops[i]
		
		if currentShop then
			local skinShopMarker = createMarker(currentShop[1], currentShop[2], currentShop[3], "cylinder", 1.5, 0, 180, 255, 75)
			setElementData(skinShopMarker, "markerID-->skinShop", currentShop[24])
			setElementDimension(skinShopMarker, currentShop[4])
			setElementInterior(skinShopMarker, currentShop[5])
			addEventHandler("onMarkerHit", skinShopMarker, selectSkin)
		end
	end
end)]]

function selectSkin (player)
	if getElementType(player) == "player" and not isPedInVehicle(player) and not isPedDead(player) then
		current = getElementData(source, "markerID-->skinShop")
		setElementFrozen ( player, true )
		showCursor ( player, true ) 
		bindKey ( player, "backspace", "down", cancelBuy ) 
		bindKey ( player, "enter", "down", buySkin ) 
		bindKey ( player, "arrow_l", "down", changeSkin ) 
		bindKey ( player, "arrow_r", "down", changeSkin )
		fadeCamera ( player, true ) 
		index [player] = 1 
		setElementPosition ( player, skinShops[current][6], skinShops[current][7], skinShops[current][8] )
		setTimer(function()
			setPedRotation ( player, skinShops[current][9] )
		end, 500, 1, player)
		setElementDimension (player, skinShops[current][10])
		setElementInterior (player, skinShops[current][11])
		setCameraMatrix ( player, skinShops[current][12], skinShops[current][13], skinShops[current][14], skinShops[current][15], skinShops[current][16], skinShops[current][17] )
		originalSkin = getElementModel( player )
	else
		cancelEvent()
	end
end
addEvent("selectSkin", true)
addEventHandler("selectSkin", root, selectSkin)
  
function cancelBuy ( player ) 
	setElementFrozen ( player, false ) 
    showCursor ( player, false ) 
    setElementPosition ( player, skinShops[current][18], skinShops[current][19], skinShops[current][20] ) 
    setPedRotation ( player, skinShops[current][21] )
	setElementDimension(player, skinShops[current][22])
	setElementInterior(player, skinShops[current][23])
    setCameraTarget ( player, player )
	setElementModel ( player, originalSkin )
	
	unbindKey ( player, "backspace", "down", cancelBuy ) 
    unbindKey ( player, "enter", "down", buySkin ) 
    unbindKey ( player, "arrow_l", "down", changeSkin ) 
    unbindKey ( player, "arrow_r", "down", changeSkin ) 
end 
  
function changeSkin ( player, key ) 
    if (key == "arrow_l") then 
        if (index [player] == 1) then 
            index [player] = #availableSkins 
        else 
            index [player] = (index[player] - 1) 
        end 
    else 
        if (index [player] == #availableSkins) then 
            index [player] = 1 
        else 
            index [player] = (index[player] + 1) 
        end 
    end
    setElementModel (player, availableSkins[index [player]]) 
    playSoundFrontEnd (player, 32)
end 
  
  
function buySkin ( player ) 
	setElementFrozen ( player, false )
    showCursor ( player, false ) 
    playSoundFrontEnd ( player, 44 )
    unbindKey ( player, "backspace", "down", cancelBuy ) 
    unbindKey ( player, "enter", "down", buySkin ) 
    unbindKey ( player, "arrow_l", "down", changeSkin ) 
    unbindKey ( player, "arrow_r", "down", changeSkin ) 
    
    setElementPosition ( player, skinShops[current][18], skinShops[current][19], skinShops[current][20] ) 
    setPedRotation ( player, skinShops[current][21] )
	setElementDimension(player, skinShops[current][22])
	setElementInterior(player, skinShops[current][23])
    setCameraTarget ( player, player )
	takePlayerMoney(player, 50)
end

And I get this error when I hit the marker:

shopS.lua:29: attempt to index field '?' (a nil value)

And when I hit backspace to exit the shop I get the same but at line 47. These lines are the setElementPosition(player ...) lines in the selectSkin and the cancelBuy functions. What could be the problem and how to fix it?

Link to comment

Client side (send the marker id to the server side)

addEventHandler("onClientResourceStart", resourceRoot, 
    function()
	    for k, v in pairs(skinShops) do
            local skinShopBlip = createBlip(v[1], v[2], v[3], 45, 2, 0, 180, 255, 255)
            local skinShopMarker = createMarker(v[1], v[2], v[3], "cylinder", 1.5, 0, 180, 255, 75)
            setElementData(skinShopMarker, "markerID-->skinShop", v[24])
			setElementDimension(skinShopMarker, v[4])
            setElementInterior(skinShopMarker, v[5])
		   
		    
            addEventHandler("onClientRender", root, 
			function()
                exports.vice_util:dxDrawTextOnElement(skinShopMarker, "Ruhabolt", 1, 20, 255, 255, 255, 255, 2, "default-bold")
            end)
           
            addEventHandler("onClientMarkerHit", skinShopMarker, function(hitPlayer, matchingDimension)
			local id = getElementData(source, "markerID-->skinShop")
			triggerServerEvent("selectSkin", hitPlayer,id)
		end)
	end
end)

Server side

function selectSkin (id)
    if getElementType(source) == "player" and not isPedInVehicle(source) and not isPedDead(source) then
        if id and tonumber(id) then
            current = id
            setElementFrozen ( source, true )
            showCursor ( source, true ) 
            bindKey ( source, "backspace", "down", cancelBuy ) 
            bindKey ( source, "enter", "down", buySkin ) 
            bindKey ( source, "arrow_l", "down", changeSkin ) 
            bindKey ( source, "arrow_r", "down", changeSkin )
            fadeCamera ( source, true ) 
            index [source] = 1 
            setElementPosition ( source, skinShops[current][6], skinShops[current][7], skinShops[current][8] )
            setTimer(function(player)
                setPedRotation ( player, skinShops[current][9] )
            end, 500, 1, source)
            setElementDimension (source, skinShops[current][10])
            setElementInterior (source, skinShops[current][11])
            setCameraMatrix ( source, skinShops[current][12], skinShops[current][13], skinShops[current][14], skinShops[current][15], skinShops[current][16], skinShops[current][17] )
            originalSkin = getElementModel( source )
        end 
    end
end 
addEvent("selectSkin", true)
addEventHandler("selectSkin", root, selectSkin)

BTW you can use the table index instead of v[24].

Edited by Walid
Link to comment

I would like to ask for help with one more thing. I was trying to do the skins depending on the shop, so this double table thing, but I don't really understand how should I get the skins inside the second table. I tried one method but it is not working, how I could do that?

function changeSkin ( player, key ) 
    if (key == "arrow_l") then 
        if (index [player] == 1) then 
            index [player] = skinShops[current][#skins]
        else 
            index [player] = (index[player] - 1) 
        end 
    else 
        if (index [player] == skinShops[current][#skins]) then 
            index [player] = 1 
        else 
            index [player] = (index[player] + 1) 
        end 
    end
    setElementModel (player, skinShops[current][skins][index [player]]) 
    playSoundFrontEnd (player, 32)
end 

And here's the tables

skinShops =
	{ -- markerX, markerY, markerZ, markerDimension, markerInterior, previewX, previewY, previewZ, previewRot, previewDimension, previewInterior, previewCamX, previewCamY, previewCamZ, previewCamLX, previewCamLY, previewCamLZ, exitPosX, exitPosY, exitposZ, markerID
  		--marker #1
		{413.72509765625, 1039.0155029297, 18, 0, 0, --shopMarkerPos, dimension, interior
		418.73413085938, 1038.1110839844, 25.25609588623, 61.661361694336, 0, 0, --previewPos, rot, dimension, interior
		414.91680908203, 1039.669921875, 26.514999389648, 415.83294677734, 1039.3103027344, 26.337924957275, --previewCamPos
		414.23324584961, 1042.4398193359, 25.409734725952, 5.6778540611267, 0, 0, --exitPos, rot, dimension, interior
		1, skins = {2, 8}}, --markerID
		
  		--marker #2
		{-17.091650009155, -928.99041748047, 9.45, 0, 0,
		-9.9858913421631, -928.94659423828, 10.419666290283, 184.99253845215, 0, 0,
		-10.584600448608, -933.49438476563, 10.881799697876, -10.511178970337, -932.49749755859, 10.911037445068,
		-15.293229103088, -933.56903076172, 10.419666290283, 213.12492370605, 0, 0,
		2, skins = {25, 26}},
	}

And I get this error: attempt to get length of global 'skins' (a nil value). What is the problem?

Link to comment

I'm trying to do a shop similar like this, but with weapons, and I would like to have 2 types of shops, one where I can get melee weapons (modded for tools, so a tool-shop thing) and one for normal weapons. So I have to define these 2 types I guess, which I did in the tables. I would like to make a different marker color and blip for the types. So type 1 would be normal weapon shop, type 2 would be the tool-shop, weapon shops would have the pistol blip, tool-shops would have the wrench blip and two different colors. The markers are being created, the colors are matching just like blips, they are correct, but I also made a 3D text on the markers saying which "shop" is which. But for some reason, the texts don't match, if I have for example 1 shop the type 1 shop matches, so it says "Weaponshop", but when I add a new table then it says "Tool-shop" but everything else matches, only the text changes. Why is this happening? What did I miss or do wrong?

Client side:

addEventHandler("onClientResourceStart", resourceRoot, function()
	for k, v in pairs(weaponShops) do
		local id = v[25]
		local weaponShopType = v[6]
		
		if weaponShopType == 1 then
			weaponShopTypeText = "Fegyverbolt"
			markerColorR, markerColorG, markerColorB = 150, 150, 150
			blipIcon = 6 --fegyver
			blipColorR, blipColorG, blipColorB = 150, 150, 150
		elseif weaponShopType == 2 then
			weaponShopTypeText = "Barkácsbolt"
			markerColorR, markerColorG, markerColorB = 255, 180, 0
			blipIcon = 27 --kalapács
			blipColorR, blipColorG, blipColorB = 255, 180, 0
		end
		
		local weaponShopBlip = createBlip(v[1], v[2], v[3], blipIcon, 2, blipColorR, blipColorG, blipColorB, 255)
		local weaponShopMarker = createMarker(v[1], v[2], v[3], "cylinder", 1.5, markerColorR, markerColorG, markerColorB, 75)
		--setElementData(weaponShopMarker, "markerID-->weaponShop", v[24])
		setElementDimension(weaponShopMarker, v[4])
		setElementInterior(weaponShopMarker, v[5])
		
		addEventHandler("onClientRender", root, function()
			exports.vice_util:dxDrawTextOnElement(weaponShopMarker, weaponShopTypeText, 1, 20, 255, 255, 255, 255, 2, "default-bold")
		end)
	   
		addEventHandler("onClientMarkerHit", weaponShopMarker, function(hitPlayer, matchingDimension)
			--local id = getElementData(source, "markerID-->weaponShop")
			triggerServerEvent("selectSkin", hitPlayer,id)
			--addEventHandler("onClientRender", root, drawSkinShop)
		end)
	end
end)

Tables:

weaponShops =
	{ -- markerX, markerY, markerZ, markerDimension, markerInterior, previewX, previewY, previewZ, previewRot, previewDimension, previewInterior, previewCamX, previewCamY, previewCamZ, previewCamLX, previewCamLY, previewCamLZ, exitPosX, exitPosY, exitposZ, markerID
  		--marker #1
		{364.40679931641, 1056.0571289063, 19, 0, 0, 1, --shopMarkerPos, dimension, interior
		418.73413085938, 1038.1110839844, 25.25609588623, 61.661361694336, 0, 0, --previewPos, rot, dimension, interior
		414.91680908203, 1039.669921875, 26.514999389648, 415.83294677734, 1039.3103027344, 26.337924957275, --previewCamPos
		414.23324584961, 1042.4398193359, 25.409734725952, 5.6778540611267, 0, 0, --exitPos, rot, dimension, interior
		1, weapons = {32, 29}}, --markerID, weapons
		
  		--marker #2
		{364.09176635742, 1078.3028564453, 19, 0, 0, 2,
		-11.364508628845, -929.12506103516, 10.419666290283, 180, 0, 0,
		-11.375499725342, -934.71636962891, 11.142499923706, -11.366688728333, -933.71667480469, 11.120515823364,
		-15.293229103088, -933.56903076172, 10.419666290283, 213.12492370605, 0, 0,
		2, weapons = {25, 26}},
		
		--marker #3
		{-64.162605285645, -1481.2530517578, 10, 0, 0, 1, --shopMarkerPos, dimension, interior
		418.73413085938, 1038.1110839844, 25.25609588623, 61.661361694336, 0, 0, --previewPos, rot, dimension, interior
		414.91680908203, 1039.669921875, 26.514999389648, 415.83294677734, 1039.3103027344, 26.337924957275, --previewCamPos
		414.23324584961, 1042.4398193359, 25.409734725952, 5.6778540611267, 0, 0, --exitPos, rot, dimension, interior
		3, weapons = {32, 29}},
		
		--marker #4
		{204.0777130127, -474.44369506836, 10.1, 0, 0, 2, --shopMarkerPos, dimension, interior
		418.73413085938, 1038.1110839844, 25.25609588623, 61.661361694336, 0, 0, --previewPos, rot, dimension, interior
		414.91680908203, 1039.669921875, 26.514999389648, 415.83294677734, 1039.3103027344, 26.337924957275, --previewCamPos
		414.23324584961, 1042.4398193359, 25.409734725952, 5.6778540611267, 0, 0, --exitPos, rot, dimension, interior
		4, weapons = {32, 29}},
	}

I didn't update lots of coordinates, only the marker creation coordinates, since I don't even made the weapon buying script yet, I just want to make the markers now. Also, I was trying to remove the setElementData for the marker, since I think it's unnecessary, because I can do the same with a variable, or am I wrong?

Edited by Dzsozi
Link to comment

Somehow you need to know which marker the player hit. You can do it with 2 tables and saving the marker element in one table with the other table index and loop through it. 

I would suggest you to organise the tables in a easy to read way. Like:

table = {

[1]={

marker = { 0, 0, 0 },

blip = { .......},

type = 5

}, 

[2]={

marker = { 5, 5, 5 },

blip = { .......},

type = 1

}

 

}

 

Always aim to have a JSON like structure because it's easy to loop through and read this way (ie unpack ()).

Edited by pa3ck
Link to comment

I tried it but I don't quite understand tables yet, I don't know how should I get the ID of the shop, I get this error in the client side script: attempt to index field '?' (a nil value), this refers to the line where I'm trying to get the id, so local id = v[1]

addEventHandler("onClientResourceStart", resourceRoot, function()
	for k, v in pairs(weaponShops) do
		local id = v[1]
		local weaponShopType = v[id][shopType]
		
		if weaponShopType == 1 then
			weaponShopTypeText = "Fegyverbolt"
			markerColorR, markerColorG, markerColorB = 150, 150, 150
			blipIcon = 6 --fegyver
			blipColorR, blipColorG, blipColorB = 150, 150, 150
		elseif weaponShopType == 2 then
			weaponShopTypeText = "Barkácsbolt"
			markerColorR, markerColorG, markerColorB = 255, 180, 0
			blipIcon = 27 --kalapács
			blipColorR, blipColorG, blipColorB = 255, 180, 0
		end
		
		local weaponShopBlip = createBlip(v[id][markerPos][1], v[id][markerPos][2], v[id][markerPos][3], blipIcon, 2, blipColorR, blipColorG, blipColorB, 255)
		local weaponShopMarker = createMarker(v[id][markerPos][1], v[id][markerPos][2], v[id][markerPos][3], "cylinder", 1.5, markerColorR, markerColorG, markerColorB, 75)
		--setElementData(weaponShopMarker, "markerID-->weaponShop", v[24])
		setElementDimension(weaponShopMarker, v[id][markerPos][4])
		setElementInterior(weaponShopMarker, v[id][markerPos][5])
		
		addEventHandler("onClientRender", root, function()
			exports.vice_util:dxDrawTextOnElement(weaponShopMarker, weaponShopTypeText, 1, 20, 255, 255, 255, 255, 2, "default-bold")
		end)
	   
		addEventHandler("onClientMarkerHit", weaponShopMarker, function(hitPlayer, matchingDimension)
			--local id = getElementData(source, "markerID-->weaponShop")
			triggerServerEvent("selectWeapon", hitPlayer,id)
			--addEventHandler("onClientRender", root, drawSkinShop)
		end)
	end
end)

Here's how I did the tables:

weaponShops =
	{ -- markerX, markerY, markerZ, markerDimension, markerInterior, previewX, previewY, previewZ, previewRot, previewDimension, previewInterior, previewCamX, previewCamY, previewCamZ, previewCamLX, previewCamLY, previewCamLZ, exitPosX, exitPosY, exitposZ, markerID
  		--marker #1
		[1] = {
		markerPos = {364.40679931641, 1056.0571289063, 18.2, 0, 0}, --shopMarkerPos, dimension, interior
		previewPos = {418.73413085938, 1038.1110839844, 25.25609588623, 61.661361694336, 0, 0}, --previewPos, rot, dimension, interior
		previewCam = {414.91680908203, 1039.669921875, 26.514999389648, 415.83294677734, 1039.3103027344, 26.337924957275}, --previewCamPos
		exitPos = {414.23324584961, 1042.4398193359, 25.409734725952, 5.6778540611267, 0, 0}, --exitPos, rot, dimension, interior
		weapons = {32, 29}, --weapons
		shopType = 1
		},
		
		[2] = {
		markerPos = {364.09176635742, 1078.3028564453, 18.1, 0, 0}, --shopMarkerPos, dimension, interior
		previewPos = {-11.364508628845, -929.12506103516, 10.419666290283, 180, 0, 0}, --previewPos, rot, dimension, interior
		previewCam = {-11.375499725342, -934.71636962891, 11.142499923706, -11.366688728333, -933.71667480469, 11.120515823364}, --previewCamPos
		exitPos = {-15.293229103088, -933.56903076172, 10.419666290283, 213.12492370605, 0, 0}, --exitPos, rot, dimension, interior
		weapons = {25, 26}, --weapons
		shopType = 2
		},
		
		[3] = {
		markerPos = {-64.162605285645, -1481.2530517578, 10, 0, 0}, --shopMarkerPos, dimension, interior
		previewPos = {418.73413085938, 1038.1110839844, 25.25609588623, 61.661361694336, 0, 0}, --previewPos, rot, dimension, interior
		previewCam = {414.91680908203, 1039.669921875, 26.514999389648, 415.83294677734, 1039.3103027344, 26.337924957275}, --previewCamPos
		exitPos = {414.23324584961, 1042.4398193359, 25.409734725952, 5.6778540611267, 0, 0}, --exitPos, rot, dimension, interior
		weapons = {25, 26}, --weapons
		shopType = 1
		},
		
		[4] = {
		markerPos = {204.0777130127, -474.44369506836, 10.1, 0, 0}, --shopMarkerPos, dimension, interior
		previewPos = {418.73413085938, 1038.1110839844, 25.25609588623, 61.661361694336, 0, 0}, --previewPos, rot, dimension, interior
		previewCam = {414.91680908203, 1039.669921875, 26.514999389648, 415.83294677734, 1039.3103027344, 26.337924957275}, --previewCamPos
		exitPos = {414.23324584961, 1042.4398193359, 25.409734725952, 5.6778540611267, 0, 0}, --exitPos, rot, dimension, interior
		weapons = {25, 26}, --weapons
		shopType = 2
		}
	}

What did I do wrong?

Edited by Dzsozi
Link to comment

That's not how you loop through tables.

for k, v = for key, value

So k is the index or key and v is the value. In your case, v is the table itself, so if you run this on your table:

for k, v in ipairs(weaponShops) do    
    
    print("Marker: ", unpack(v.markerPos))
    print("Preview: ", unpack(v.previewPos))
    print("Exit: ", unpack(v.exitPos))
    print("Weapons: ", unpack(v.weapons))

end

You will get something like this back.

Link to comment

Sorry for the late response, I didn't have time for trying to fix it, but now when I tried again in several ways, I still don't know how should I fix it or do that. I just don't understand why is this happening. The marker colors and blips are good, but the 3d text that is on the marker is not. For some reason, if I have 2 shops, it writes the text of weapon shop type number two for every maker, but if I have 3 shops, so tables, it writes the text of weapon shop type number one for every marker. But the colors of the markers, the blip icons are matching. Why is this happening? Hope you can understand me.

addEventHandler("onClientResourceStart", resourceRoot, function()
	for k, v in ipairs(weaponShops) do
		local weaponShopType = v.shopType
		
		if weaponShopType == 1 then
			weaponShopTypeText = "Fegyverbolt" -- <- this is the weaponshop text for shop type number 1, it would have weapons in it
			markerColorR, markerColorG, markerColorB = 150, 150, 150
			blipIcon = 6 --weapon
			blipColorR, blipColorG, blipColorB = 150, 150, 150
		elseif weaponShopType == 2 then
			weaponShopTypeText = "Barkácsbolt" -- <- this is the toolshop text for shop type number 2, it would have tools in it
			markerColorR, markerColorG, markerColorB = 255, 180, 0
			blipIcon = 27 --wrench
			blipColorR, blipColorG, blipColorB = 255, 180, 0
		end
		
		local weaponShopBlip = createBlip(v.markerPos[1], v.markerPos[2], v.markerPos[3], blipIcon, 2, blipColorR, blipColorG, blipColorB, 255)
		local weaponShopMarker = createMarker(v.markerPos[1], v.markerPos[2], v.markerPos[3], "cylinder", 1.5, markerColorR, markerColorG, markerColorB, 75)
		setElementDimension(weaponShopMarker, v.markerPos[4])
		setElementInterior(weaponShopMarker, v.markerPos[5])
		
		addEventHandler("onClientRender", root, function()
			exports.vice_util:dxDrawTextOnElement(weaponShopMarker, weaponShopTypeText, 1, 20, 255, 255, 255, 255, 2, "default-bold")
		end)
	   
		addEventHandler("onClientMarkerHit", weaponShopMarker, function(hitPlayer, matchingDimension)
			triggerServerEvent("selectWeapon", hitPlayer,id)
		end)
	end
end)

So, I have this here, shop type number one has a grey marker color and the pistol blip icon, and shop type number two has an orange marker color and the wrench blip icon, they are matching and working as it should, but for some reason every marker has the "Barkácsbolt" text if I have 4 shops, but if I delete one shop, so if I have 3 every marker has the "Fegyverbolt" text. Why is this?

weaponShops =
	{ -- markerX, markerY, markerZ, markerDimension, markerInterior, previewX, previewY, previewZ, previewRot, previewDimension, previewInterior, previewCamX, previewCamY, previewCamZ, previewCamLX, previewCamLY, previewCamLZ, exitPosX, exitPosY, exitposZ, markerID
  		--marker #1
		[1] = {
		markerPos = {364.40679931641, 1056.0571289063, 18.2, 0, 0}, --shopMarkerPos, dimension, interior
		previewPos = {418.73413085938, 1038.1110839844, 25.25609588623, 61.661361694336, 0, 0}, --previewPos, rot, dimension, interior
		previewCam = {414.91680908203, 1039.669921875, 26.514999389648, 415.83294677734, 1039.3103027344, 26.337924957275}, --previewCamPos
		exitPos = {414.23324584961, 1042.4398193359, 25.409734725952, 5.6778540611267, 0, 0}, --exitPos, rot, dimension, interior
		weapons = {32, 29}, --weapons
		shopType = 1
		},
		
		[2] = {
		markerPos = {364.09176635742, 1078.3028564453, 18.1, 0, 0}, --shopMarkerPos, dimension, interior
		previewPos = {-11.364508628845, -929.12506103516, 10.419666290283, 180, 0, 0}, --previewPos, rot, dimension, interior
		previewCam = {-11.375499725342, -934.71636962891, 11.142499923706, -11.366688728333, -933.71667480469, 11.120515823364}, --previewCamPos
		exitPos = {-15.293229103088, -933.56903076172, 10.419666290283, 213.12492370605, 0, 0}, --exitPos, rot, dimension, interior
		weapons = {25, 26}, --weapons
		shopType = 2
		},
		
		[3] = {
		markerPos = {-64.162605285645, -1481.2530517578, 10, 0, 0}, --shopMarkerPos, dimension, interior
		previewPos = {418.73413085938, 1038.1110839844, 25.25609588623, 61.661361694336, 0, 0}, --previewPos, rot, dimension, interior
		previewCam = {414.91680908203, 1039.669921875, 26.514999389648, 415.83294677734, 1039.3103027344, 26.337924957275}, --previewCamPos
		exitPos = {414.23324584961, 1042.4398193359, 25.409734725952, 5.6778540611267, 0, 0}, --exitPos, rot, dimension, interior
		weapons = {25, 26}, --weapons
		shopType = 1
		},
		
		[4] = {
		markerPos = {204.0777130127, -474.44369506836, 10.1, 0, 0}, --shopMarkerPos, dimension, interior
		previewPos = {418.73413085938, 1038.1110839844, 25.25609588623, 61.661361694336, 0, 0}, --previewPos, rot, dimension, interior
		previewCam = {414.91680908203, 1039.669921875, 26.514999389648, 415.83294677734, 1039.3103027344, 26.337924957275}, --previewCamPos
		exitPos = {414.23324584961, 1042.4398193359, 25.409734725952, 5.6778540611267, 0, 0}, --exitPos, rot, dimension, interior
		weapons = {25, 26}, --weapons
		shopType = 2
		}
	}

 

Link to comment
		if weaponShopType == 1 then
			weaponShopTypeText = "Fegyverbolt" -- <- this is the weaponshop text for shop type number 1, it would have weapons in it
			markerColorR, markerColorG, markerColorB = 150, 150, 150
			blipIcon = 6 --weapon
			blipColorR, blipColorG, blipColorB = 150, 150, 150
  			outputChatBox("created type - weapon")
		elseif weaponShopType == 2 then
			weaponShopTypeText = "Barkácsbolt" -- <- this is the toolshop text for shop type number 2, it would have tools in it
			markerColorR, markerColorG, markerColorB = 255, 180, 0
			blipIcon = 27 --wrench
			blipColorR, blipColorG, blipColorB = 255, 180, 0
  			outputChatBox("created type - tools")
		end

See what the outputChatBox returns. 

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