Jump to content

Arrest script


Recommended Posts

So i'm currently scripting a arrest system\resource for my server.
When the police officer enters a vehicle the arrested criminal will be warped into the vehicle.

But how can I make the arrested criminal to leave the vehicle when the police officer leaves the vehicle?

Spoiler

function policeStations(thePolice, thePrisoner, underArrest)
	if (underArrest == true) then
		policeArrestLSmarker = createMarker (1542.12, -1719.37, 12.55, "cylinder", 2, 0, 0, 255, 120)
		setElementVisibleTo (policeArrestLSmarker, root, false)
		setElementVisibleTo (policeArrestLSmarker, thePolice, true)
		policeArrestLSblip = createBlip (1542.12, -1719.37, 12.55, 30, 2)
		setElementVisibleTo (policeArrestLSblip, root, false)
		setElementVisibleTo (policeArrestLSblip, thePolice, true)
	elseif (underArrest == false) then
		destroyElement (policeArrestLSmarker)
		destroyElement (policeArrestLSblip)
	end
end

function onPlayerWantedlevelHit(theAttacker, theWeapon)
	if (theAttacker) and (theWeapon == 3) then
		if getTeamName(getPlayerTeam(theAttacker)) == "Police Officer" then
			local wlevel = getPlayerWantedLevel (source)
			local PrisonerArrestState = getElementData (source, "policeArrest:isPrisonerArrested")
			local PoliceArrestState = getElementData (theAttacker, "policeArrest:hasPolicePrisoner")
			if ((PrisonerArrestState) == false) and (wlevel > 0) then
				local underArrest = true
				policeStations (theAttacker, source, underArrest)
				setElementData (source, "policeArrest:isPrisonerArrested", true)
				setElementData (theAttacker, "policeArrest:hasPolicePrisoner", true)
				setElementData (theAttacker, "policeArrest:arrestedWho", source)
				setElementParent (theAttacker, source)
				showCursor (source, true)
				toggleAllControls (source, false, true, false)
				followPoliceOfficer (theAttacker, source)
				setElementData (source, "arrestTimeout", setTimer (freeThePrisoner, 20000, 1, source, theAttacker))
				-- 1200000
			end
		end
	end
end
addEventHandler ("onPlayerDamage", getRootElement (), onPlayerWantedlevelHit)

function followPoliceOfficer(thePolice, thePrisoner)
	if (getElementData (thePrisoner, "policeArrest:isPrisonerArrested") == true) and (getPedOccupiedVehicle (thePrisoner) == false) then
		local policeX, policeY, policeZ = getElementPosition (thePolice)
		local prisonerX, prisonerY, prisonerZ = getElementPosition (thePrisoner)
		copangle = (360 - math.deg (math.atan2 ((policeX - prisonerX), (policeY - prisonerY)))) % 360
		setPedRotation (thePrisoner, copangle)
		setCameraTarget (thePrisoner, thePrisoner)
		local distance = getDistanceBetweenPoints2D (policeX, policeY, prisonerX, prisonerY)
		if (distance > 16) then
			local policeX, policeY, policeZ = getElementPosition (thePolice)
			setElementPosition (thePrisoner, policeX - 1, policeY, policeZ)
			setControlState (thePrisoner, "sprint", true)
			setControlState (thePrisoner, "walk", false)
			setControlState (thePrisoner, "forwards", true)
			local zombify = setTimer (followPoliceOfficer, 300, 1, thePolice, thePrisoner)
		elseif (distance > 12) then
			setControlState (thePrisoner, "sprint", true)
			setControlState (thePrisoner, "walk", false)
			setControlState (thePrisoner, "forwards", true)
			local zombify = setTimer (followPoliceOfficer, 300, 1, thePolice, thePrisoner)
		elseif (distance > 6) then
			setControlState (thePrisoner, "sprint", false)
			setControlState (thePrisoner, "walk", false)
			setControlState (thePrisoner, "forwards", true)
			local zombify = setTimer (followPoliceOfficer, 300, 1, thePolice, thePrisoner)
		elseif (distance > 1.5) then
			setControlState (thePrisoner, "sprint", false)
			setControlState (thePrisoner, "walk", true)
			setControlState (thePrisoner, "forwards", true)
			local zombify = setTimer (followPoliceOfficer, 300, 1, thePolice, thePrisoner)
		elseif (distance < 1.5) then
			setControlState (thePrisoner, "sprint", false)
			setControlState (thePrisoner, "walk", false)
			setControlState (thePrisoner, "forwards", false)
			if isPedInVehicle (thePolice) then
				theVehicle = getPedOccupiedVehicle (thePolice)
				if (getVehicleOccupant (theVehicle, 2) == false) then
					warpPedIntoVehicle (thePrisoner, theVehicle, 2)
				elseif (getVehicleOccupant (theVehicle, 3) == false) then
					warpPedIntoVehicle (thePrisoner, theVehicle, 3)
				end
--			else
--				removePedFromVehicle (thePrisoner)
			end
			local zombify = setTimer (followPoliceOfficer, 300, 1, thePolice, thePrisoner)
		end
	end
end

function freeThePrisoner (thePrisoner, thePolice)
	setElementData (thePrisoner, "policeArrest:isPrisonerArrested", false)
	setElementData (thePrisoner, "policeArrest:arrestedWho", false)
	showCursor (thePrisoner, false)
	setControlState (thePrisoner, "sprint", false)
	setControlState (thePrisoner, "walk", false)
	setControlState (thePrisoner, "forwards", false)
	toggleAllControls (thePrisoner, true)
	setElementData (thePolice, "policeArrest:hasPolicePrisoner", false)
	exports["GTARPGtopBar"]:sendClientMessage ("The arrested player has been released!", thePolice, 255, 0, 0, true, 15)
	exports["GTARPGtopBar"]:sendClientMessage ("You took to long to bring the criminal to the station!", thePolice, 255, 0, 0, true, 15)
	exports["GTARPGtopBar"]:sendClientMessage ("The police officer was not able to arrest you.", thePrisoner, 255, 255, 255, true, 10)
	exports["GTARPGtopBar"]:sendClientMessage ("You are free to go for now!", thePrisoner, 0, 255, 0, true, 10)
	local underArrest = false
	policeStations (thePolice, thePrisoner, underArrest)
	TimoutTimer = (getElementData (thePrisoner, "arrestTimeout"))
	if isTimer(TimoutTimer) then
		 killTimer(TimoutTimer)
		 setElementData (thePrisoner, "arrestTimeout", nil)
		 TimeoutTimer = nil
	end
end

function freeThePrisonerOnQuit()
	if (getElementData (source, "currentstatus") == "underarrest") then
		local players = getElementsByType ("player")
		for theKey,thePlayer in ipairs(players) do
			if (getElementData (thePlayer, "policeArrest:arrestedWho") == source) then
				freeThePrisoner (thePlayer)
				exports["GTARPGtopBar"]:sendClientMessage ("The police officer has left.", thePrisoner, 255, 255, 255, true, 8)
				exports["GTARPGtopBar"]:sendClientMessage ("You are free to go for now!", thePrisoner, 0, 255, 0, true, 8)
			end
		end
	end
end
addEventHandler ("onPlayerQuit", getRootElement(), freeThePrisonerOnQuit)

--[[
function enableControls(thePlayer)
	showCursor (thePlayer, false)
	setControlState (thePlayer, "sprint", false)
	setControlState (thePlayer, "walk", false)
	setControlState (thePlayer, "forwards", false)
	toggleAllControls (thePlayer, true)
	setElementData (thePlayer, "policeArrest:isPrisonerArrested", false)
	setElementData (thePlayer, "policeArrest:arrestedWho", false)
end
addCommandHandler ("free", enableControls)
]]--

 

 

Edited by Potato_Tomato420
Link to comment

When i use removePlayerFromVehicle it will spawn the arrested criminal inside the vehicle.

Can i use removePlayerFromVehicle and then setElementPosition to spawn the arrested criminal near the vehicle instead in the vehicle? Of is there a better way to handle this?

Like:

setControlState (thePrisoner, "enter_exit", true)
Edited by Potato_Tomato420
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...