Custom traffic lights are weak

Hey guys,

I need some help with custom traffic lights (YFT). When i put them into my server they are as weak as paper.
When you walk into them they fall over, is there any way i can make them indestructible? Or is it something i need to change inside of the XML?

Please let me know if there is a fix for this! :slight_smile:

Greetings

PS: I saw some other topics about this but they didn’t really help me

Something like that would be achieved via adjusting the flags for those assets. If it’s a YMAP file that places the lights, use CodeWalker - that’ll be by far the most straight-forward method of adjusting flags.

Hey thanks for your fast reply,

These traffic lights replace the normal traffic lights spawned around the map. So they are not an YMAP.
Is it possible to add flags when replacing textures?

And do you by any chance know which flag to add? :smiley:

Off the top of my head, it should be flag “32” (that’s “static_prop” (?))
Can you send the file(s) that do that? I dont particularly understand the way you’re replacing them :smiley:

Traffic lights.zip (433.8 KB)

Here are the files, there are multiple traffic light files but i just added one to check it out.
I stream these through my resources

Okay, this is a rather weird one, and I hate to say it but I have no idea how you’d fix that. There dont seem to be any natives that would explicitly add a specific flag to any entity (there is one for vehicles, if I understand it correctly, but there’s no way it can get used for this case)

No problem man, thank you for the help trying to figure this out!
I hope someone who knows what the problem is sees this and helps me out.

If it’s considered as prop you can apply a FreezeEntityPosition() (FREEZE_ENTITY_POSITION - Cfx.re Docs) on it no?

I suppose that is a option yes, i then need to make a script that performs that function on all traffic lights. I will look into that. Thank you for the help!

you can try this:

local props = {
	"prop_name1",
	"prop_name2",
}

Citizen.CreateThread(function()
	local propsHash = {}
	for i=1,#props do
		propsHash[GetHashKey(props[i])] = true
	end
	while true do
		Citizen.Wait(500)
		local objects = GetAllObjects()
		for i=1,#objects do
			if propsHash[GetEntityModel(objects[i])] then
				FreezeEntityPosition(objects[i])
			end
		end
	end
end)

Place your models in the table at top, tell me if i made misstake (not tested).

1 Like

It did not work for me, or i used it completely wrong :sweat_smile:
Are you willing to test it out yourself?


local entityEnumerator = {
	__gc = function(enum)
		if enum.destructor and enum.handle then
			enum.destructor(enum.handle)
		end

		enum.destructor = nil
		enum.handle = nil
	end
}

local function EnumerateEntities(initFunc, moveFunc, disposeFunc)
	return coroutine.wrap(function()
		local iter, id = initFunc()
		if not id or id == 0 then
			disposeFunc(iter)
			return
		end

		local enum = {handle = iter, destructor = disposeFunc}
		setmetatable(enum, entityEnumerator)

		local next = true
		repeat
		coroutine.yield(id)
		next, id = moveFunc(iter)
		until not next

		enum.destructor, enum.handle = nil, nil
		disposeFunc(iter)
	end)
end

function EnumerateObjects()
	return EnumerateEntities(FindFirstObject, FindNextObject, EndFindObject)
end

local props = {
	"prop_traffic_01a",
}

Citizen.CreateThread(function()
	local propsHash = {}
	for i=1,#props do
		propsHash[GetHashKey(props[i])] = true
		print (GetHashKey(props[i]))
	end
	while true do
		Citizen.Wait(500)
        for v in EnumerateObjects() do
			if propsHash[GetEntityModel(v)] then
				FreezeEntityPosition(v, true)
				SetEntityCanBeDamaged(v, false)
			end
		end
	end
end)

This work, i must use ESX functions enumerator for entities, dont known why GetAllObjects doesn’t work :confused:
But warning…

Yes thank you!! It worked! Thank you for the help :grin:

Prayers for an answer but how did you make the file to let it work. I am currently trying to make it work with the answer but to no success.

Optimised version of this would be:

local entityEnumerator = {
	__gc = function(enum)
		if enum.destructor and enum.handle then
			enum.destructor(enum.handle)
		end

		enum.destructor = nil
		enum.handle = nil
	end
}

local function EnumerateEntities(initFunc, moveFunc, disposeFunc)
	return coroutine.wrap(function()
		local iter, id = initFunc()
		if not id or id == 0 then
			disposeFunc(iter)
			return
		end

		local enum = {handle = iter, destructor = disposeFunc}
		setmetatable(enum, entityEnumerator)

		local next = true
		repeat
		coroutine.yield(id)
		next, id = moveFunc(iter)
		until not next

		enum.destructor, enum.handle = nil, nil
		disposeFunc(iter)
	end)
end

function EnumerateObjects()
	return EnumerateEntities(FindFirstObject, FindNextObject, EndFindObject)
end

local props = {
	"prop_traffic_01a",
	"prop_traffic_03a",
	"prop_traffic_01b",
	"prop_traffic_01d",
	"prop_traffic_03b"
}

local enumeratedObjects = nil
local cachedPlayerCoords = vector3(0,0,0)

CreateThread(function()
	local propsHash = {}
	for i=1,#props do
		propsHash[GetHashKey(props[i])] = true
		-- print (GetHashKey(props[i]))
	end
	while true do
		sleepThread = 500

		local player = PlayerPedId()
		local pCoords = GetEntityCoords(player)

		local moveDst = #(pCoords - cachedPlayerCoords)

		if moveDst >= 50.0 then
			print("Triggerd")
			cachedPlayerCoords = pCoords

			for v in EnumerateObjects() do
				if propsHash[GetEntityModel(v)] then
					FreezeEntityPosition(v, true)
					SetEntityCanBeDamaged(v, false)
				end
			end
		end

		Wait(sleepThread)
	end
end)

It’s very performance heavy to check the close by objects every 500ms there for you should only check if the distance the player has moved is atleast above 50