Jump to content

[HELP] Tables


xXGhostXx

Recommended Posts

Hi guys.

What is problem in my table ?

local FacVehsTable = {
	{ 1, "Police" },
	{ 2, "Medic" },
}

function FactionVehEnter ( player, seat, jacked ) 
	if seat == 0 then
		local vehid = getElementID ( source )
		local faction = getElementData(player, "faction")
		if vehid == FacVehsTable[faction][2] then
		if faction ~= FacVehsTable[faction][1] then
			cancelEvent()
			outputChatBox ( "#ff0000This is faction vehicle!, player, 230,0,0 ,true) 
		end
	end
	end
end
addEventHandler ( "onVehicleStartEnter", getRootElement(), FactionVehEnter )

I want when player enter the faction cars if not "faction" element data player == faction id vehicles then cancelEvent 

Link to comment
  • Scripting Moderators
1 hour ago, xXGhostXx said:

Hi guys.

What is problem in my table ?


local FacVehsTable = {
	{ 1, "Police" },
	{ 2, "Medic" },
}

function FactionVehEnter ( player, seat, jacked ) 
	if seat == 0 then
		local vehid = getElementID ( source )
		local faction = getElementData(player, "faction")
		if vehid == FacVehsTable[faction][2] then
		if faction ~= FacVehsTable[faction][1] then
			cancelEvent()
			outputChatBox ( "#ff0000This is faction vehicle!, player, 230,0,0 ,true) 
		end
	end
	end
end
addEventHandler ( "onVehicleStartEnter", getRootElement(), FactionVehEnter )

I want when player enter the faction cars if not "faction" element data player == faction id vehicles then cancelEvent 

This doesn't work like that. You need to change table structure or loop whole table, i suggest 1st option. Let me know if something wouldn't work.

local facVehsTable = {
	[1] = "Police",
	[2] = "Medic",
}

--

function onVehicleStartEnter(player, seat, jacked, door)
	if seat == 0 then
		local vehid = getElementID(source)
		local faction = getElementData(player)
		if facVehsTable[vehid] then -- if vehid is equal to 1 or 2
			if faction ~= facVehsTable[vehid] then -- if faction isn't equal to Police or Medic
				cancelEvent()
				outputChatBox("#ff0000This is faction vehicle!", player, 230, 0, 0, true)
			end
		end
	end
end
addEventHandler("onVehicleStartEnter", getRootElement(), onVehicleStartEnter)

 

Link to comment
3 minutes ago, majqq said:

This doesn't work like that. You need to change table structure or loop whole table, i suggest 1st option. Let me know if something wouldn't work.


local facVehsTable = {
	[1] = "Police",
	[2] = "Medic",
}

--

function onVehicleStartEnter(player, seat, jacked, door)
	if seat == 0 then
		local vehid = getElementID(source)
		local faction = getElementData(player)
		if facVehsTable[vehid] then -- if vehid is equal to 1 or 2
			if faction ~= facVehsTable[vehid] then -- if faction isn't equal to Police or Medic
				cancelEvent()
				outputChatBox("#ff0000This is faction vehicle!", player, 230, 0, 0, true)
			end
		end
	end
end
addEventHandler("onVehicleStartEnter", getRootElement(), onVehicleStartEnter)

 

Thanks but i want [1] for faction id and "Police" element car id.

If my faction id = 1 for enter "Police" car id or if my faction id = 2 for enter "Medic" car id. 

Link to comment
  • Scripting Moderators
4 minutes ago, xXGhostXx said:

Thanks but i want [1] for faction id and "Police" element car id.

If my faction id = 1 for enter "Police" car id or if my faction id = 2 for enter "Medic" car id. 

Try like that:

local facVehsTable = {
	["Police"] = 1,
	["Medic"] = 2,
}

--

function onVehicleStartEnter(player, seat, jacked, door)
	if seat == 0 then
		local vehid = getElementID(source)
		local faction = getElementData(player)
		if facVehsTable[vehid] then -- if vehid is equal to Police or Medic
			if faction ~= facVehsTable[vehid] then -- if faction isn't equal to Police(1) or Medic(2)
				cancelEvent()
				outputChatBox("#ff0000This is faction vehicle!", player, 230, 0, 0, true)
			end
		end
	end
end
addEventHandler("onVehicleStartEnter", getRootElement(), onVehicleStartEnter)

 

Link to comment
12 minutes ago, majqq said:

Try like that:


local facVehsTable = {
	["Police"] = 1,
	["Medic"] = 2,
}

--

function onVehicleStartEnter(player, seat, jacked, door)
	if seat == 0 then
		local vehid = getElementID(source)
		local faction = getElementData(player)
		if facVehsTable[vehid] then -- if vehid is equal to Police or Medic
			if faction ~= facVehsTable[vehid] then -- if faction isn't equal to Police(1) or Medic(2)
				cancelEvent()
				outputChatBox("#ff0000This is faction vehicle!", player, 230, 0, 0, true)
			end
		end
	end
end
addEventHandler("onVehicleStartEnter", getRootElement(), onVehicleStartEnter)

 

Thanks, how can i set 2 faction for some cars.

for example police department and federal police can enter the "Police" vehicle but Medic cant enter the car.

Edited by xXGhostXx
Link to comment
  • Scripting Moderators
4 hours ago, xXGhostXx said:

Thanks, how can i set 2 faction for some cars.

for example police department and federal police can enter the "Police" vehicle but Medic cant enter the car.

I think it's easy to do, you just need to modify table.

Link to comment
  • Scripting Moderators
8 hours ago, xXGhostXx said:

How ?

Please give me?

Try:

I've also fixed my mistake with elementData.

local facVehsTable = {
	["Police"] = {
		[1] = true, -- faction: 1, can enter
		[3] = true, -- faction: 3, can enter
		[4] = false, -- faction: 4, can't enter
	},
	["Medic"] = {
		[2] = true, -- faction: 2, can enter
	},
}

--

function onVehicleStartEnter(player, seat, jacked, door)
	if seat == 0 then
		local vehid = getElementID(source)
		local faction = getElementData(player, "faction") -- mistake, fixed it
		if facVehsTable[vehid] then -- if vehid is equal to Police or Medic
			if not faction or not facVehsTable[vehid][faction] then -- if server can't get faction, or player faction isn't equal to one of in table
				cancelEvent()
				outputChatBox("#ff0000This is faction vehicle!", player, 230, 0, 0, true)
			end
		end
	end
end
addEventHandler("onVehicleStartEnter", getRootElement(), onVehicleStartEnter)

 

Link to comment
On 09/10/2019 at 22:36, majqq said:

Try:

I've also fixed my mistake with elementData.


local facVehsTable = {
	["Police"] = {
		[1] = true, -- faction: 1, can enter
		[3] = true, -- faction: 3, can enter
		[4] = false, -- faction: 4, can't enter
	},
	["Medic"] = {
		[2] = true, -- faction: 2, can enter
	},
}

--

function onVehicleStartEnter(player, seat, jacked, door)
	if seat == 0 then
		local vehid = getElementID(source)
		local faction = getElementData(player, "faction") -- mistake, fixed it
		if facVehsTable[vehid] then -- if vehid is equal to Police or Medic
			if not faction or not facVehsTable[vehid][faction] then -- if server can't get faction, or player faction isn't equal to one of in table
				cancelEvent()
				outputChatBox("#ff0000This is faction vehicle!", player, 230, 0, 0, true)
			end
		end
	end
end
addEventHandler("onVehicleStartEnter", getRootElement(), onVehicleStartEnter)

 

Thanks for your help.

I have a problem.

I trying to make respawn faction vehicles.

For example: i am police officer and i want respawn my faction cars or i am medic and i want respawn my faction cars.

addCommandHandler("rv",
function(thePlayer, command)
	for index, Fac_Cars in ipairs(getElementsByType("vehicle")) do 
		local vehid = getElementID(Fac_Cars)
		local faction = getElementData(thePlayer, "faction")
		if facVehsTable[vehid] then
			respawnVehicle(Fac_Cars)
		end
	end
end)

What is problem ?

Edited by xXGhostXx
Link to comment
  • Scripting Moderators
5 hours ago, xXGhostXx said:

Thanks for your help.

I have a problem.

I trying to make respawn faction vehicles.

For example: i am police officer and i want respawn my faction cars or i am medic and i want respawn my faction cars.


addCommandHandler("rv",
function(thePlayer, command)
	for index, Fac_Cars in ipairs(getElementsByType("vehicle")) do 
		local vehid = getElementID(Fac_Cars)
		local faction = getElementData(thePlayer, "faction")
		if facVehsTable[vehid] then
			respawnVehicle(Fac_Cars)
		end
	end
end)

What is problem ?

Use outputDebugString to check if code actually runs.

Add it before condition, and inside condition.

Maybe you didn't set spawn coordinates - not sure.

https://wiki.multitheftauto.com/wiki/SetVehicleRespawnPosition

Also use int loop not ipairs, ipairs isn't that effective.

addCommandHandler("rv", 
	function(player, cmd)
		local vehicles = getElementsByType("vehicle")
		for i = 1, #vehicles do
			local vehicle = vehicles[i]
			if vehicle then
				local vehid = getElementID(vehicle)
				--local faction = getElementData(player, "faction") - why do you need this? i commented this because it's never used
				if facVehsTable[vehid] then
					respawnVehicle(vehicle)
				end
			end
		end
	end
end)

 

Edited by majqq
Link to comment
15 hours ago, majqq said:

Use outputDebugString to check if code actually runs.

Add it before condition, and inside condition.

Maybe you didn't set spawn coordinates - not sure.

https://wiki.multitheftauto.com/wiki/SetVehicleRespawnPosition

Also use int loop not ipairs, ipairs isn't that effective.


addCommandHandler("rv", 
	function(player, cmd)
		local vehicles = getElementsByType("vehicle")
		for i = 1, #vehicles do
			local vehicle = vehicles[i]
			if vehicle then
				local vehid = getElementID(vehicle)
				--local faction = getElementData(player, "faction") - why do you need this? i commented this because it's never used
				if facVehsTable[vehid] then
					respawnVehicle(vehicle)
				end
			end
		end
	end
end)

 

Dear i want if you are police faction you can respawn police cars or if you are medic faction respawn medic cars with /rv command !

And "local faction = getElementData(player, "faction")" for get my faction for respawn my faction cars

Edited by xXGhostXx
Link to comment
  • Scripting Moderators
5 hours ago, xXGhostXx said:

Dear i want if you are police faction you can respawn police cars or if you are medic faction respawn medic cars with /rv command !

And "local faction = getElementData(player, "faction")" for get my faction for respawn my faction cars

Uncomment this:

--local faction = getElementData(player, "faction") - why do you need this? i commented this because it's never used

 

So it will look like:

local faction = getElementData(player, "faction")

And move it under or above:

local vehicles = getElementsByType("vehicle")

To not necessarily call function x times in loop.

After that you need to index:

[faction]

On line 10:

if facVehsTable[vehid] then

This will use data from facVehsTable.

Goodluck.

Link to comment
42 minutes ago, majqq said:

Uncomment this:


--local faction = getElementData(player, "faction") - why do you need this? i commented this because it's never used

 

So it will look like:


local faction = getElementData(player, "faction")

And move it under or above:


local vehicles = getElementsByType("vehicle")

To not necessarily call function x times in loop.

After that you need to index:


[faction]

On line 10:


if facVehsTable[vehid] then

This will use data from facVehsTable.

Goodluck.

Yes i fixed but i want police faction respawn police cars.

FBI faction respawn fbi cars.

or ...

addCommandHandler("rv",
function (player, command)
			local vehicles = getElementsByType("vehicle")
			for i = 1, #vehicles do
				local vehicle = vehicles[i]
				if vehicle then
					local vehid = getElementID(vehicle)
					local faction = getElementData(player, "faction")
					if facVehsTable[vehid] and facVehsTable[vehid][faction] then
							respawnVehicle(vehicle)
					end
				end
			end
end)

In this code i want get my faction and find in the table my faction id and get the car and respawn car geted!

Link to comment
  • Scripting Moderators
18 minutes ago, xXGhostXx said:

Yes i fixed but i want police faction respawn police cars.

FBI faction respawn fbi cars.

or ...

Create other table for it, you have an example how you can do it.

Besides this should look like this:

addCommandHandler("rv", 
	function(player, cmd)
		local faction = getElementData(player, "faction")
		local vehicles = getElementsByType("vehicle")
		for i = 1, #vehicles do
			local vehicle = vehicles[i]
			if vehicle then
				local vehid = getElementID(vehicle)
				if facVehsTable[vehid][faction] then
					respawnVehicle(vehicle)
				end
			end
		end
	end
end)

 

Link to comment
17 hours ago, majqq said:

Create other table for it, you have an example how you can do it.

Besides this should look like this:


addCommandHandler("rv", 
	function(player, cmd)
		local faction = getElementData(player, "faction")
		local vehicles = getElementsByType("vehicle")
		for i = 1, #vehicles do
			local vehicle = vehicles[i]
			if vehicle then
				local vehid = getElementID(vehicle)
				if facVehsTable[vehid][faction] then
					respawnVehicle(vehicle)
				end
			end
		end
	end
end)

 

Thanks you can give me table for this code ?

Link to comment
  • Scripting Moderators
42 minutes ago, xXGhostXx said:

Thanks you can give me table for this code ?

No, create another one by yourself, it should looks same as facVehsTable but adjust it to your needs.

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