Jump to content

trancar veiculos mta rp


Recommended Posts

Spoiler

 

--[[



--]]

local types = {
	["Automobile"] = true,
	["Bike"] = true,
	["Plane"] = true,
	["Helicopter"] = true,
	["Boat"] = true,
	["Train"] = true,
	["Monster Truck"] = true,
	["Quad"] = true
}

 


local beltVehicles = {
	["Automobile"] = true,
	["Plane"] = true,
	["Helicopter"] = true,
	["Boat"] = true,
	["Train"] = true,
	["Monster Truck"] = true
}


function playSounds(sounds, bool)
	if not bool then bool = false end
	if sounds == "ov" then
		beltSound = playSound("files/".. sounds ..".mp3", bool)
	else
	    playSound("files/".. sounds ..".mp3", bool)
	end
end

local beltVehicles = {
	["Automobile"] = true,
	["Plane"] = true,
	["Helicopter"] = true,
	["Boat"] = true,
	["Train"] = true,
	["Monster Truck"] = true
}

addEventHandler("onClientVehicleEnter", root, function(player)
	if player == localPlayer and beltVehicles[getVehicleType(getPedOccupiedVehicle(localPlayer))] then
		setElementData(localPlayer, "veh:ov", false)
	end
end)


addEventHandler("onClientVehicleExit", root, function(player)
	if player == localPlayer then
		if isElement(beltSound) then
		end
	end
end)


addEventHandler("onClientKey", root, function(key, pressed)
	if isChatBoxInputActive() or isConsoleActive() then return end
	if key == "x" and pressed and getPedOccupiedVehicle(localPlayer) and beltVehicles[getVehicleType(getPedOccupiedVehicle(localPlayer))] then
		if not getElementData(localPlayer, "veh:ov") then
			setElementData(localPlayer, "veh:ov", true)
			playSound("files/ovbe.mp3", false)
			outputChatBox("#1066E7[BVH - Veiculo]: #FFFFFFVocê colocou o cinto de segurança!", 255, 255, 255, true)
		else
			setElementData(localPlayer, "veh:ov", false)
			playSound("files/ovki.mp3", false)
			outputChatBox("#1066E7[BVH - Veiculo]: #FFFFFFVocê tirou o cinto de segurança!", 255, 255, 255, true)
		end
	end
end)


addEventHandler("onClientElementDestroy", getRootElement(), function ()
	if getElementType(source) == "vehicle" and types[getVehicleType(source)] and getVehicleType(source) ~= "Bike" then
		setElementData(localPlayer, "veh:ov", false)
	end
end)

addEventHandler("onClientPlayerVehicleEnter", localPlayer, function(vehicle, seat)
	if source == localPlayer then
		local engine = vehicle:getData("veh:motor")
		local vehicle = getPedOccupiedVehicle(localPlayer)
		
		if getVehicleType(getPedOccupiedVehicle(localPlayer)) == "BMX" then
			setVehicleEngineState(vehicle, true)
		else
			if not engine then
				setVehicleEngineState(vehicle, false)
			else
				setVehicleEngineState(vehicle, true)
			end
		end
	end
end)


addEventHandler("onClientPlayerVehicleEnter", localPlayer, function(vehicle, seat)
	if source == localPlayer then
		local engine = vehicle:getData("veh:motor")
		local vehicle = getPedOccupiedVehicle(localPlayer)
		
		if getVehicleType(getPedOccupiedVehicle(localPlayer)) == "BMX" then
			setVehicleEngineState(vehicle, true)
		else
			if not engine then
				setVehicleEngineState(vehicle, false)
			else
				setVehicleEngineState(vehicle, true)
			end
		end
		if vehicle then
		outputChatBox("#1066E7[BVH - Veiculo]: #FFFFFFPara ligar o veículo, pressione #1066E7J", 255, 255, 255, true)
		outputChatBox("#1066E7[BVH - Veiculo]: #FFFFFFColoque o cinto de segurança, pressione #1066E7X", 255, 255, 255, true)
		end
	end
end)

function keyHandler(button,state) --Função Ligar motor
	if not (not guiGetInputEnabled() and not isMTAWindowActive() and not isCursorShowing()) then return end
	if isPedInVehicle(localPlayer) and getPedOccupiedVehicleSeat(localPlayer) == 0 then
		if not types[getVehicleType(getPedOccupiedVehicle(localPlayer))] then return end
		
		local veh = getPedOccupiedVehicle(localPlayer)
		if button == "j" and state then
				if isPedInVehicle(localPlayer) and getPedOccupiedVehicleSeat(localPlayer) == 0 then
					
					local state = getVehicleEngineState ( veh )
					if not state then
						playSounds("lightswitch")
						vehTimer = setTimer(kocsiindit,1000,1)
						playSounds("starter")
					else
						kocsiindit()
					end

				end
		end
	end	
end
addEventHandler("onClientKey",getRootElement(),keyHandler)

function kocsiindit()
	local veh = getPedOccupiedVehicle(localPlayer)
	local state = getVehicleEngineState ( veh )
		if not state then
			m = true
			 
			if getElementHealth(veh) >= 321 then --Verifica se o carro ta quebrado se sim
				m = true
			else
				m = false
                outputChatBox("#1066E7[BVH - Veiculo]: #FFFFFFParece que está com o motor fundido chame por um Mecânico /mecanicos!", 255, 255, 255, true)				
			end
			
			if getElementData(veh, "Gasolina") > 0 then --Verifica se o veiculo ta sem gasolina se sim
			    m = true
				outputChatBox("#1066E7[BVH - Veiculo]: #FFFFFFVeiculo ligado!", 255, 255, 255, true)
			else
			    m = false
                outputChatBox("#1066E7[BVH - Veiculo]: #FFFFFFO Veiculo está sem gasolina", 255, 255, 255, true)				
			end

			else
			   m = false
			   outputChatBox("#1066E7[BVH - Veiculo]: #FFFFFFVeiculo desligado!", 255, 255, 255, true)		
		end
		triggerServerEvent("vehicleStart",localPlayer,localPlayer,veh,m)
end

function processLockUnlock(vehicle)
local locked = isVehicleLocked(vehicle)
	if isPedInVehicle(localPlayer) then
	playSounds("lockin")
	else
	playSounds("lockout")
	end
	if locked then
	triggerServerEvent("vehicleLock", localPlayer,localPlayer, vehicle, false)
	outputChatBox("#1066E7[BVH - Veiculo]: #FFFFFFVeiculo destrancado!", 255, 255, 255, true)
	else
	triggerServerEvent("vehicleLock", localPlayer,localPlayer, vehicle, true)
	outputChatBox("#1066E7[BVH - Veiculo]: #FFFFFFVeiculo trancado!", 255, 255, 255, true)
	end
end

function light() --Função do farol
	if klikkTimer then return end
	if isTimer(klikkTimerRun) then return end
	klikkTimer = true
	klikkTimerRun = setTimer(function()
		klikkTimer = false
	end,1000,1)
	local vehicle = getPedOccupiedVehicle(localPlayer)
	if isPedInVehicle(localPlayer) and getVehicleController(vehicle) == localPlayer and getVehicleType(vehicle) ~= "BMX" then
		local vehicle = getPedOccupiedVehicle ( localPlayer )
		local vehLampaStat = tonumber(vehicle:getData("veh:light")) or 0
		if vehLampaStat == 0 then
			playSounds("lightswitch")
			triggerServerEvent("setVehLightState",vehicle,vehicle,2, localPlayer)
			vehicle:setData("veh:light",1)
			vehicle:setData("veh >> light",true)
		else
			triggerServerEvent("setVehLightState",vehicle,vehicle,1, localPlayer)
			vehicle:setData("veh:light",0)
			playSounds("lightswitch")
			vehicle:setData("veh >> light",false)
		end
	end
end
bindKey("l", "down", light)
local klikkTimer = false

function setVehicleLockState() --Função trancar o carro
	if klikkTimer then return end
	if isTimer(klikkTimerRun) then return end
	klikkTimer = true
	klikkTimerRun = setTimer(function()
		klikkTimer = false
	end,1000,1)
	local vehicle = getPedOccupiedVehicle ( localPlayer )
	if vehicle then
		processLockUnlock(vehicle)
	else
		local int2 = getElementInterior(localPlayer)
		local dim2 = getElementDimension(localPlayer)
		local mx,my,mz = getElementPosition(localPlayer)
			for k,v in ipairs(getElementsByType("vehicle")) do
				local x,y,z = getElementPosition(v)
				local int = getElementInterior(v)
				local dim = getElementDimension(v)
				local dist = getDistanceBetweenPoints3D(x,y,z,mx,my,mz)
				if dist <= 3 and int2 == int and dim2 == dim then
					processLockUnlock(v)
					return
				end
			end
	end
end
bindKey("k", "down", setVehicleLockState)

bom esté é o client do meu mod de trancar veiculos, o problema é que nos veiculos da conce, outros jogadores conseguem destrancar os veiculos um dos outros

algm tem a moral de me ajudar com isso aqui 

Edited by Lord Henry
Código convertido de HTML para Lua e colocado dentro de Spoiler.
Link to comment
On 15/02/2022 at 21:37, Duff1995 said:
  Reveal hidden contents

 

--[[



--]]

local types = {
	["Automobile"] = true,
	["Bike"] = true,
	["Plane"] = true,
	["Helicopter"] = true,
	["Boat"] = true,
	["Train"] = true,
	["Monster Truck"] = true,
	["Quad"] = true
}

 


local beltVehicles = {
	["Automobile"] = true,
	["Plane"] = true,
	["Helicopter"] = true,
	["Boat"] = true,
	["Train"] = true,
	["Monster Truck"] = true
}


function playSounds(sounds, bool)
	if not bool then bool = false end
	if sounds == "ov" then
		beltSound = playSound("files/".. sounds ..".mp3", bool)
	else
	    playSound("files/".. sounds ..".mp3", bool)
	end
end

local beltVehicles = {
	["Automobile"] = true,
	["Plane"] = true,
	["Helicopter"] = true,
	["Boat"] = true,
	["Train"] = true,
	["Monster Truck"] = true
}

addEventHandler("onClientVehicleEnter", root, function(player)
	if player == localPlayer and beltVehicles[getVehicleType(getPedOccupiedVehicle(localPlayer))] then
		setElementData(localPlayer, "veh:ov", false)
	end
end)


addEventHandler("onClientVehicleExit", root, function(player)
	if player == localPlayer then
		if isElement(beltSound) then
		end
	end
end)


addEventHandler("onClientKey", root, function(key, pressed)
	if isChatBoxInputActive() or isConsoleActive() then return end
	if key == "x" and pressed and getPedOccupiedVehicle(localPlayer) and beltVehicles[getVehicleType(getPedOccupiedVehicle(localPlayer))] then
		if not getElementData(localPlayer, "veh:ov") then
			setElementData(localPlayer, "veh:ov", true)
			playSound("files/ovbe.mp3", false)
			outputChatBox("#1066E7[BVH - Veiculo]: #FFFFFFVocê colocou o cinto de segurança!", 255, 255, 255, true)
		else
			setElementData(localPlayer, "veh:ov", false)
			playSound("files/ovki.mp3", false)
			outputChatBox("#1066E7[BVH - Veiculo]: #FFFFFFVocê tirou o cinto de segurança!", 255, 255, 255, true)
		end
	end
end)


addEventHandler("onClientElementDestroy", getRootElement(), function ()
	if getElementType(source) == "vehicle" and types[getVehicleType(source)] and getVehicleType(source) ~= "Bike" then
		setElementData(localPlayer, "veh:ov", false)
	end
end)

addEventHandler("onClientPlayerVehicleEnter", localPlayer, function(vehicle, seat)
	if source == localPlayer then
		local engine = vehicle:getData("veh:motor")
		local vehicle = getPedOccupiedVehicle(localPlayer)
		
		if getVehicleType(getPedOccupiedVehicle(localPlayer)) == "BMX" then
			setVehicleEngineState(vehicle, true)
		else
			if not engine then
				setVehicleEngineState(vehicle, false)
			else
				setVehicleEngineState(vehicle, true)
			end
		end
	end
end)


addEventHandler("onClientPlayerVehicleEnter", localPlayer, function(vehicle, seat)
	if source == localPlayer then
		local engine = vehicle:getData("veh:motor")
		local vehicle = getPedOccupiedVehicle(localPlayer)
		
		if getVehicleType(getPedOccupiedVehicle(localPlayer)) == "BMX" then
			setVehicleEngineState(vehicle, true)
		else
			if not engine then
				setVehicleEngineState(vehicle, false)
			else
				setVehicleEngineState(vehicle, true)
			end
		end
		if vehicle then
		outputChatBox("#1066E7[BVH - Veiculo]: #FFFFFFPara ligar o veículo, pressione #1066E7J", 255, 255, 255, true)
		outputChatBox("#1066E7[BVH - Veiculo]: #FFFFFFColoque o cinto de segurança, pressione #1066E7X", 255, 255, 255, true)
		end
	end
end)

function keyHandler(button,state) --Função Ligar motor
	if not (not guiGetInputEnabled() and not isMTAWindowActive() and not isCursorShowing()) then return end
	if isPedInVehicle(localPlayer) and getPedOccupiedVehicleSeat(localPlayer) == 0 then
		if not types[getVehicleType(getPedOccupiedVehicle(localPlayer))] then return end
		
		local veh = getPedOccupiedVehicle(localPlayer)
		if button == "j" and state then
				if isPedInVehicle(localPlayer) and getPedOccupiedVehicleSeat(localPlayer) == 0 then
					
					local state = getVehicleEngineState ( veh )
					if not state then
						playSounds("lightswitch")
						vehTimer = setTimer(kocsiindit,1000,1)
						playSounds("starter")
					else
						kocsiindit()
					end

				end
		end
	end	
end
addEventHandler("onClientKey",getRootElement(),keyHandler)

function kocsiindit()
	local veh = getPedOccupiedVehicle(localPlayer)
	local state = getVehicleEngineState ( veh )
		if not state then
			m = true
			 
			if getElementHealth(veh) >= 321 then --Verifica se o carro ta quebrado se sim
				m = true
			else
				m = false
                outputChatBox("#1066E7[BVH - Veiculo]: #FFFFFFParece que está com o motor fundido chame por um Mecânico /mecanicos!", 255, 255, 255, true)				
			end
			
			if getElementData(veh, "Gasolina") > 0 then --Verifica se o veiculo ta sem gasolina se sim
			    m = true
				outputChatBox("#1066E7[BVH - Veiculo]: #FFFFFFVeiculo ligado!", 255, 255, 255, true)
			else
			    m = false
                outputChatBox("#1066E7[BVH - Veiculo]: #FFFFFFO Veiculo está sem gasolina", 255, 255, 255, true)				
			end

			else
			   m = false
			   outputChatBox("#1066E7[BVH - Veiculo]: #FFFFFFVeiculo desligado!", 255, 255, 255, true)		
		end
		triggerServerEvent("vehicleStart",localPlayer,localPlayer,veh,m)
end

function processLockUnlock(vehicle)
local locked = isVehicleLocked(vehicle)
	if isPedInVehicle(localPlayer) then
	playSounds("lockin")
	else
	playSounds("lockout")
	end
	if locked then
	triggerServerEvent("vehicleLock", localPlayer,localPlayer, vehicle, false)
	outputChatBox("#1066E7[BVH - Veiculo]: #FFFFFFVeiculo destrancado!", 255, 255, 255, true)
	else
	triggerServerEvent("vehicleLock", localPlayer,localPlayer, vehicle, true)
	outputChatBox("#1066E7[BVH - Veiculo]: #FFFFFFVeiculo trancado!", 255, 255, 255, true)
	end
end

function light() --Função do farol
	if klikkTimer then return end
	if isTimer(klikkTimerRun) then return end
	klikkTimer = true
	klikkTimerRun = setTimer(function()
		klikkTimer = false
	end,1000,1)
	local vehicle = getPedOccupiedVehicle(localPlayer)
	if isPedInVehicle(localPlayer) and getVehicleController(vehicle) == localPlayer and getVehicleType(vehicle) ~= "BMX" then
		local vehicle = getPedOccupiedVehicle ( localPlayer )
		local vehLampaStat = tonumber(vehicle:getData("veh:light")) or 0
		if vehLampaStat == 0 then
			playSounds("lightswitch")
			triggerServerEvent("setVehLightState",vehicle,vehicle,2, localPlayer)
			vehicle:setData("veh:light",1)
			vehicle:setData("veh >> light",true)
		else
			triggerServerEvent("setVehLightState",vehicle,vehicle,1, localPlayer)
			vehicle:setData("veh:light",0)
			playSounds("lightswitch")
			vehicle:setData("veh >> light",false)
		end
	end
end
bindKey("l", "down", light)
local klikkTimer = false

function setVehicleLockState() --Função trancar o carro
	if klikkTimer then return end
	if isTimer(klikkTimerRun) then return end
	klikkTimer = true
	klikkTimerRun = setTimer(function()
		klikkTimer = false
	end,1000,1)
	local vehicle = getPedOccupiedVehicle ( localPlayer )
	if vehicle then
		processLockUnlock(vehicle)
	else
		local int2 = getElementInterior(localPlayer)
		local dim2 = getElementDimension(localPlayer)
		local mx,my,mz = getElementPosition(localPlayer)
			for k,v in ipairs(getElementsByType("vehicle")) do
				local x,y,z = getElementPosition(v)
				local int = getElementInterior(v)
				local dim = getElementDimension(v)
				local dist = getDistanceBetweenPoints3D(x,y,z,mx,my,mz)
				if dist <= 3 and int2 == int and dim2 == dim then
					processLockUnlock(v)
					return
				end
			end
	end
end
bindKey("k", "down", setVehicleLockState)

bom esté é o client do meu mod de trancar veiculos, o problema é que nos veiculos da conce, outros jogadores conseguem destrancar os veiculos um dos outros

algm tem a moral de me ajudar com isso aqui 

Oi,

Acredito que você deva utilizar um outro sistema de trancar veículos. Vi que no código que enviou não há nada armazenando o veículo e que possa associá-lo à um jogador. Além de que não é viável utilizar as funções de trancar veículos em client-side.

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