Jump to content

Lock System


shaio

Recommended Posts

And I'm tackling my advanced lock system again.. I want to save tables to the player instead of the car, so that each time a player spawns a vehicle the player can remotely lock and unlock their vehicle, and possibly in the future kick specific players out of it remotely.

So here is my code, and the only error seems to be on line 275, please help me fix this.

 

root = getRootElement()
personalCar = {}

addEventHandler("onResourceStart",root,function()
	for _,plr in pairs(getElementsByType("player")) do
		bindKey(plr,"k","down","lock")
	end
end)

addEventHandler("onPlayerJoin",root,function()
	bindKey(source,"k","down","lock")
end)

function newCar(player,cmd,carName,value,value2)
	if isPedInVehicle(player) then
		local car = getPedOccupiedVehicle(player)
		destroyElement(car)
	end
	local x,y,z = getElementPosition(player)
	if tonumber(carName) then
		local vehicle = createVehicle(carName,x,y,z+1)
		warpPedIntoVehicle(player,vehicle)
		addVehicle(player,vehicle)
	else
		if carName == "super" then
			if value == "gt" then
				local vehicle = createVehicle(506,x,y,z+1)
				warpPedIntoVehicle(player,vehicle)
				addVehicle(player,vehicle)
			end
		elseif carName == "blista" then
			if value == "compact" then
				local vehicle = createVehicle(496,x,y,z+1)
				warpPedIntoVehicle(player,vehicle)
				addVehicle(player,vehicle)
			end
		elseif carName == "news" then
			if value == "chopper" then
				local vehicle = createVehicle(488,x,y,z+1)
				warpPedIntoVehicle(player,vehicle)
				addVehicle(player,vehicle)
			elseif value == "van" then
				local vehicle = createVehicle(582,x,y,z+1)
				warpPedIntoVehicle(player,vehicle)
				addVehicle(player,vehicle)
			end
		elseif carName == "police" then
			if value == "maverick" then
				local vehicle = createVehicle(497,x,y,z+1)
				warpPedIntoVehicle(player,vehicle)
				addVehicle(player,vehicle)
			elseif value == "ls" then
				local vehicle = createVehicle(596,x,y,z+1)
				warpPedIntoVehicle(player,vehicle)
				addVehicle(player,vehicle)
			elseif value == "sf" then
				local vehicle = createVehicle(597,x,y,z+1)
				warpPedIntoVehicle(player,vehicle)
				addVehicle(player,vehicle)
			elseif value == "lv" then
				local vehicle = createVehicle(598,x,y,z+1)
				warpPedIntoVehicle(player,vehicle)
				addVehicle(player,vehicle)
			elseif value == "ranger" then
				local vehicle = createVehicle(599,x,y,z+1)
				warpPedIntoVehicle(player,vehicle)
				addVehicle(player,vehicle)
			end
		elseif carName == "mountain" then
			if value == "bike" then
				local vehicle = createVehicle(510,x,y,z+1)
				warpPedIntoVehicle(player,vehicle)
				addVehicle(player,vehicle)
			end
		elseif carName == "utility" then
			if value == "van" then
				local vehicle = createVehicle(552,x,y,z+1)
				warpPedIntoVehicle(player,vehicle)
				addVehicle(player,vehicle)
			end
		elseif carName == "fbi" then
			if value == "rancher" then
				local vehicle = createVehicle(490,x,y,z+1)
				warpPedIntoVehicle(player,vehicle)
				addVehicle(player,vehicle)
			elseif value == "truck" then
				local vehicle = createVehicle(528,x,y,z+1)
				warpPedIntoVehicle(player,vehicle)
				addVehicle(player,vehicle)
			end
		elseif carName == "combine" then
			if value == "harvester" then
				local vehicle = createVehicle(532,x,y,z+1)
				warpPedIntoVehicle(player,vehicle)
				addVehicle(player,vehicle)
			end
		elseif carName == "black" then
			if value == "boxville" then
				local vehicle = createVehicle(609,x,y,z+1)
				warpPedIntoVehicle(player,vehicle)
				addVehicle(player,vehicle)
			end
		elseif carName == "mr" then
			if value == "whoopee" then
				local vehicle = createVehicle(423,x,y,z+1)
				warpPedIntoVehicle(player,vehicle)
				addVehicle(player,vehicle)
			end
		elseif carName == "hotring" then
			if value == "racer" then
				if value2 == "2" then
					local vehicle = createVehicle(502,x,y,z+1)
					warpPedIntoVehicle(player,vehicle)
				addVehicle(player,vehicle)
				elseif value2 == "3" then
					local vehicle = createVehicle(503,x,y,z+1)
					warpPedIntoVehicle(player,vehicle)
				addVehicle(player,vehicle)
				elseif not value2 then
					local vehicle = createVehicle(494,x,y,z+1)
					warpPedIntoVehicle(player,vehicle)
				addVehicle(player,vehicle)
				end
			end
		elseif carName == "monster" then
			if value == "2" then
				local vehicle = createVehicle(556,x,y,z+1)
				warpPedIntoVehicle(player,vehicle)
				addVehicle(player,vehicle)
			elseif value == "3" then
				local vehicle = createVehicle(557,x,y,z+1)
				warpPedIntoVehicle(player,vehicle)
				addVehicle(player,vehicle)
			elseif not value then
				local vehicle = createVehicle(444,x,y,z+1)
				warpPedIntoVehicle(player,vehicle)
				addVehicle(player,vehicle)
			end
		elseif carName == "trailer" then
			if value == "2" then
				local vehicle = createVehicle(608,x,y,z+1)
				warpPedIntoVehicle(player,vehicle)
				addVehicle(player,vehicle)
			elseif value == "3" then
				local vehicle = createVehicle(435,x,y,z+1)
				warpPedIntoVehicle(player,vehicle)
				addVehicle(player,vehicle)
			elseif value == "4" then
				local vehicle = createVehicle(450,x,y,z+1)
				warpPedIntoVehicle(player,vehicle)
				addVehicle(player,vehicle)
			elseif value == "5" then
				local vehicle = createVehicle(591,x,y,z+1)
				warpPedIntoVehicle(player,vehicle)
				addVehicle(player,vehicle)
			elseif not value then
				local vehicle = createVehicle(611,x,y,z+1)
				warpPedIntoVehicle(player,vehicle)
				addVehicle(player,vehicle)
			end
		elseif carName == "baggage" then
			if value == "trailer" then
				if value2 == "2" then
					local vehicle = createVehicle(607,x,y,z+1)
					warpPedIntoVehicle(player,vehicle)
					addVehicle(player,vehicle)
				elseif not value2 then
					local vehicle = createVehicle(606,x,y,z+1)
					warpPedIntoVehicle(player,vehicle)
					addVehicle(player,vehicle)
				end
			end
		elseif carName == "farm" then
			if value == "trailer" then
				local vehicle = createVehicle(610,x,y,z+1)
				warpPedIntoVehicle(player,vehicle)
				addVehicle(player,vehicle)
			end
		elseif carName == "petrol" then
			if value == "trailer" then
				local vehicle = createVehicle(584,x,y,z+1)
				warpPedIntoVehicle(player,vehicle)
				addVehicle(player,vehicle)
			end
		elseif carName == "rc" then
			if value == "bandit" then
				local vehicle = createVehicle(441,x,y,z+1)
				warpPedIntoVehicle(player,vehicle)
				addVehicle(player,vehicle)
			elseif value == "baron" then
				local vehicle = createVehicle(464,x,y,z+1)
				warpPedIntoVehicle(player,vehicle)
				addVehicle(player,vehicle)
			elseif value == "cam" then
				local vehicle = createVehicle(594,x,y,z+1)
				warpPedIntoVehicle(player,vehicle)
				addVehicle(player,vehicle)
			elseif value == "goblin" then
				local vehicle = createVehicle(501,x,y,z+1)
				warpPedIntoVehicle(player,vehicle)
				addVehicle(player,vehicle)
			elseif value == "raider" then
				local vehicle = createVehicle(495,x,y,z+1)
				warpPedIntoVehicle(player,vehicle)
				addVehicle(player,vehicle)
			elseif value == "tiger" then
				local vehicle = createVehicle(564,x,y,z+1)
				warpPedIntoVehicle(player,vehicle)
				addVehicle(player,vehicle)
			elseif value == "van" then
				local vehicle = createVehicle(459,x,y,z+1)
				warpPedIntoVehicle(player,vehicle)
				addVehicle(player,vehicle)
			end
		elseif carName == "box" then
			if value == "freight" then
				local vehicle = createVehicle(590,x,y,z+1)
				warpPedIntoVehicle(player,vehicle)
				addVehicle(player,vehicle)
			end
		elseif carName == "brown" then
			if value == "streak" then
				if value2 == "carriage" then
					local vehicle = createVehicle(570,x,y,z+1)
					warpPedIntoVehicle(player,vehicle)
					addVehicle(player,vehicle)
				elseif not value2 then
					local vehicle = createVehicle(538,x,y,z+1)
					warpPedIntoVehicle(player,vehicle)
					addVehicle(player,vehicle)
				end
			end
		elseif carName == "flat" then
			if value == "freight" then
				local vehicle = createVehicle(569,x,y,z+1)
				warpPedIntoVehicle(player,vehicle)
				addVehicle(player,vehicle)
			end
		elseif carName == "bloodring" then
			if value == "banger" then
				local vehicle = createVehicle(504,x,y,z+1)
				warpPedIntoVehicle(player,vehicle)
				addVehicle(player,vehicle)
			end
		elseif carName == "bf" then
			if value == "injection" then
				local vehicle = createVehicle(424,x,y,z+1)
				warpPedIntoVehicle(player,vehicle)
				addVehicle(player,vehicle)
			end
		else
			local vehicle = createVehicle(getVehicleIDFromName(carName),x,y,z+1)
			warpPedIntoVehicle(player,vehicle)
			addVehicle(player,vehicle)
		end
	end
end
addCommandHandler("vi",newCar)

function lock(player,cmd)
    if personalCar[player] then
        if personalCar[player].locked == true then
            personalCar[player].locked = false
            outputChatBox("Your vehicle is now unlocked!",player,0,255,255)
        elseif personalCar[player].locked == false then
            personalCar[player].locked = true
            outputChatBox("Your vehicle is now locked!",player,0,255,255)
        end
    end
end
addCommandHandler("lock",lock)

addEventHandler("onVehicleStartEnter",root,function(player,seat)
    if seat == 0 then
		if not source == personalCar[player].v then
			if personalCar[player].locked == true then
				cancelEvent()
				outputChatBox("This vehicle is locked!",player,0,255,255)
            end
        end
    end
end)

function eject(player,cmd,other)
	if isPedInVehicle(player) then
		if getVehicleOccupant(getPedOccupiedVehicle(player),0) == player then
			if (getPlayerName(getPlayerFromName(other)) == (getPlayerName(player))) then
				outputChatBox("You cannot eject yourself!",player,255,0,0,true)
			else
				removePlayerFromVehicle(getPlayerFromName(other))
				local x,y,z = getElementPosition(getPlayerFromName(other))
				setElementPosition(getPlayerFromName(other),x+5,y,z)
				outputChatBox(getPlayerName(player).." has ejected you from the vehicle!",getPlayerFromName(other),0,255,255,true)
				local vehicle = getPedOccupiedVehicle(player)
				for _,plr in ipairs(getVehicleOccupants(vehicle)) do
					outputChatBox(getPlayerName(player).." has ejected "..getPlayerName(getPlayerFromName(other)),plr,0,255,255,true)
				end
			end
		else
			outputChatBox("You need to be the driver!",player,255,0,0)
		end
	end
end
addCommandHandler("eject",eject)

function addVehicle(player,vehicle)
    if not personalCar[player] then
        personalCar[player] = {v = vehicle,locked = true}
    else
        personalCar[player] = {v = vehicle,locked = true}
    end
end

 

Link to comment
18 minutes ago, Gravestone said:

if source ~= personalCar[player].v then

Try this maybe.

P.s if you don't mind me saying, why are you using elseif statements every time? You can create a vehicle table and check if the text matches the name in the vehicle table.

I use elseif to check for exact values, say it = something else, that part of the function will still run. I don't want that happening.

and the ~= didn't work. Same error.

line 275: attempt to index field "?" (a nil value)

Edited by shaio
Link to comment
22 hours ago, Adolfram said:

you should check if player's value is registered on table


if personalCar[player] then --end

 

I do this under the lock/unlock portion of the script. If I do it under the onVehicleStart enter event it will be locked if the player that's trying to get in has a vehicle anyway.. So I cannot do that under the event.
I could however check if personalCar, but that would be pointless..

 

root = getRootElement()
personalCar = {}

function lock(player,cmd)
    if personalCar[player] then
        if personalCar[player].locked == true then
            personalCar[player].locked = false
            outputChatBox("Your vehicle is now unlocked!",player,0,255,255)
        elseif personalCar[player].locked == false then
            personalCar[player].locked = true
            outputChatBox("Your vehicle is now locked!",player,0,255,255)
        end
    end
end
addCommandHandler("lock",lock)

addEventHandler("onVehicleStartEnter",root,function(player,seat)
    if seat == 0 then
		if not source ~= personalCar[player].v then
			if personalCar[player].locked == true then
				cancelEvent()
				outputChatBox("This vehicle is locked!",player,0,255,255)
            end
        end
    end
end)

 

Edited by shaio
Link to comment
6 minutes ago, Adolfram said:

no, check that and then check personalCar[player].v. that's why you're getting errors, you're trying to index a nil value

This wont work.. Because if the player who's trying to access the car doesn't have a car of their own in their table, then the rest of the script wont run..

 

addEventHandler("onVehicleStartEnter",root,function(player,seat)
	if seat == 0 then
		if personalCar[player] then -- Checks if the ^ player there has a vehicle inside of their table.
			if not source ~= personalCar[player].v then -- this wont run if the player ^ up there, doesn't have a vehicle.
				if personalCar[player].locked == true then
					cancelEvent()
					outputChatBox("This vehicle is locked!",player,0,255,255)
				end
			end
		end
	end
end)

 

Edited by shaio
Link to comment

can you please try this

 

addEventHandler("onVehicleStartEnter",root,function(player,seat)
    if seat == 0 then
      	if personalCar[player] then 
          if not source ~= personalCar[player].v then
              if personalCar[player].locked == true then
                  cancelEvent()
                  outputChatBox("This vehicle is locked!",player,0,255,255)
              end
          end
        end
    end
end)

 

if you are trying to check if vehicle is locked and is not owned by the player who's entering you should

 

addEventHandler("onVehicleStartEnter",root,function(player,seat)
    if seat == 0 then
      	for i, veh in pairs (personalCar) do 
          if source == veh.v and i~= player then
              if veh.locked == true then
                  cancelEvent()
                  outputChatBox("This vehicle is locked!",player,0,255,255)
              end
          end
    end
end)

 

Edited by Adolfram
Link to comment
addEventHandler("onVehicleStartEnter",root,function(player,seat)
	if seat == 0 then
		if personalCar[player] then -- Checks if the ^ player there has a vehicle inside of their table.
			if not source ~= personalCar[player].v then -- this wont run if the player ^ up there, doesn't have a vehicle.
				if personalCar[player].locked == true then
					cancelEvent()
					outputChatBox("This vehicle is locked!",player,0,255,255)
				end
			end
		end
	end
end)

 

I'm sorry, but that just wont work..

Link to comment
3 minutes ago, Adolfram said:

edited the code, take a look please

addEventHandler("onVehicleStartEnter",root,function(player,seat)
	if (seat == 0) then
		for i,veh in pairs(personalCar) do
			if source == veh.v then
				if veh.locked == true then
					cancelEvent()
					outputChatBox("This vehicle is locked!",player,0,255,255)
				end
			end
		end
	end
end)

Actually this code won't work, I'm sorry.. It will check the vehicle yes, but not in the specific players table. I want it to check the players table for the players vehicle, and if the players vehicle is NOT the vehicle he is trying to enter, I don't want him to be able to.
But this code, it loops through the entire table, which these values only exist in personalCar[player] not personalCar, since we are creating sub tables for each player. Meaning that even if there weren't sub tables, any car that is in the table would be locked.

And the owner of the vehicle would not be able to access it after exiting.

The purpose of this code is to simply allow the owner to control the vehicle without being inside of it, or near it.

I made a simple modification, hopefully it will work, even though it seems like it's not even valid :l

addEventHandler("onVehicleStartEnter",root,function(player,seat)
	if (seat == 0) then
		if personalCar[player] then
			for i,veh in pairs(personalCar[player]) do
				if source == veh.v then
					if veh.locked == true then
						cancelEvent()
						outputChatBox("This vehicle is locked!",player,0,255,255)
					end
				end
			end
		end
	end
end)

 

Link to comment
2 minutes ago, Adolfram said:

veh represents all the sub tables, have you tried the code? because i did and it actually works, lol

 

exactly, it represents all the sub tables, which means it will check all the vehicles, and if one of those vehicles are locked, it will not work for the rest.

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