[HELP] Why does this not work? Blips on all banks

Hey guys. I can’t for the life of me figure out why my script isn’t working. What I want it to do is grab the x,y,z coords of the banks and put blips on the map for them. I have searched the forums and every example script has similar content. Here is what I have.

local banks = {
  {name="Bank", id=108, x=150.266, y=-1040.203, z=29.374},
  {name="Bank", id=108, x=-1212.980, y=-330.841, z=37.787},
  {name="Bank", id=108, x=-2962.582, y=482.627, z=15.703},
  {name="Bank", id=108, x=-112.202, y=6469.295, z=31.626},
  {name="Bank", id=108, x=314.187, y=-278.621, z=54.170},
  {name="Bank", id=108, x=-351.534, y=-49.529, z=49.042},
  {name="Bank", id=108, x=241.727, y=220.706, z=106.286},
  {name="Bank", id=108, x=1175.0643310547, y=2706.6435546875, z=38.094036102295}
}	

AddEventHandler("playerSpawned", function(spawn)
	Citizen.CreateThread(function()
		TriggerEvent("chatMessage", "", { 0, 0, 0 }, "Blips loading")
		while true do
			Citizen.Wait(0)
			for k in pairs(banks) do
				local bankBlips = AddBlipForCoord(banks[k].x, banks[k].y, banks[z].z)
				SetBlipSprite(bankBlips, banks[k].id)
				SetBlipDisplay(bankBlips, 2)
				SetBlipScale(bankBlips, 1)
				SetBlipColour(bankBlips, 2)
				SetBlipAsShortRange(bankBlips, false)
				BeginTextCommandSetBlipName("String")
				AddTextComponentString(banks[k].name)
				EndTextCommandSetBlipName(bankBlips)
			end
			TriggerEvent("chatMessage", "", { 0, 0, 0 }, "Blips loaded")
		end
	end)
end)

Uhhh in your line of the following you have banks[k].x, banks[k].y, banks[z].z. Try changing the [z] to a k?

local bankBlips = AddBlipForCoord(banks[k].x, banks[k].y, banks[k].z)

Also does it throw any errors in the F8 console?

1 Like

Nice spot, I didn’t notice that. That may be the fix although I am not home right now to fix it. Thanks man.
However, one more question. To optimize this, I want to only run it when the player connects to the server. Does my code do that right now, or every time the player spawns?

At every Spawn …

1 Like
local banks = {
  {name='Bank', id=108, x=150.266, y=-1040.203, z=29.374},
  {name='Bank', id=108, x=-1212.980, y=-330.841, z=37.787},
  {name='Bank', id=108, x=-2962.582, y=482.627, z=15.703},
  {name='Bank', id=108, x=-112.202, y=6469.295, z=31.626},
  {name='Bank', id=108, x=314.187, y=-278.621, z=54.170},
  {name='Bank', id=108, x=-351.534, y=-49.529, z=49.042},
  {name='Bank', id=108, x=241.727, y=220.706, z=106.286},
  {name='Bank', id=108, x=1175.064, y=2706.643, z=38.094}
}

local LoadedBlips = {}; FirstSpawn = false --Added this table and this variable to determine if the blips are already loaded and if this is the first spawn.

AddEventHandler('playerSpawned', function(spawn)
	if not FirstSpawn then
		TriggerEvent('chatMessage', '', { 0, 0, 0 }, 'Loading ' .. #banks .. '. Blips')
		for k, bank in pairs(banks) do
			if LoadedBlips[k] == nil or (LoadedBlips[k] ~= nil and not DoesBlipExist(LoadedBlips[k])) then --Checks if the Blip already exists
				local Blip = AddBlipForCoord(bank.x, bank.y, bank.z)
				SetBlipSprite(Blip, bank.id)
				SetBlipDisplay(Blip, 2)
				SetBlipScale(Blip, 1.0)
				SetBlipColour(Blip, 2)
				SetBlipAlpha(Blip, 255) --Set the Alpha to maximum to make the Blip visible on the map
				SetBlipAsShortRange(Blip, false)
				BeginTextCommandSetBlipName('String')
				AddTextComponentString(bank.name)
				EndTextCommandSetBlipName(Blip)
				LoadedBlips[k] = Blip --Adds the current blip to the new table
			end
		end
		TriggerEvent('chatMessage', '', { 0, 0, 0 }, 'Loaded ' .. #LoadedBlips .. '. Blips')
		FirstSpawn = true --Sets the variable to true, so this code only runs once
	end
end)
2 Likes

Looks great man, thank you for your help! Also, why do you not need to use Citizen.CreateThread? I thought you needed to have that every time.

This is just (afaik) needed if you want to run some Code asynchronous.

@jayden8250 could you please upload all the codes? would like to make blips myself, but got nu clue…

Flatracers response fixed my problem, check that out

ahh i see, is this client or server script? @jayden8250 ?

That is a client script.

thanks! will test it out now!