Jump to content

How to make a Ped enter a Vehicle by getting close to it?


Recommended Posts

This is my first post, so forgive me if i make any mistakes!

I'm creating a Police Script where i get to a random Ped and type a command(i'll change it to bind later) and it creates a Marker attached(0,1,0) to the Source that if it touches the Ped will activate a function that makes him follow the Marker. And this is the function so far:

Everything is Server-Side (because i suck at Client-Side)

function func1(p)
local x,y,z = getElementPosition(p)
Marker = createMarker(x, y, z, "cylinder", 1, 255, 0, 0, 170)
attachElements(Marker,p, 0,1,0)
addEventHandler("onMarkerHit", Marker, func2)
addEventHandler("onMarkerHit", Marker, func3)
end
addCommandHandler("render", func1)


function fun2(p,_)
if getElementType ( p ) == "ped" then
x2,y2,z2 = getElementPosition(Marker)
x1,y1,z1 = getElementPosition(p)

rx,ry,rz = findRotation3D(x1,y1,z1,x2,y2,z2)
setElementRotation(p, rx+0,ry+0,rz+0)
setPedAnimation(p, "ped", "walk_player")

setTimer(function()
bunda(p,_)
end, 999, 1)
end
end

And then here's the devil:

function func3(p, ped)
if not getElementType ( ped ) == "ped" then return end
--else return end
--carro = getElementType ( carro )
carro = getElementsByType("vehicle")
--psha = getElementColShape(p)
--elements = getElementsWithinColShape(psha)
--if elements == carro == "vehicle" then

--local vee = getElementsByType(carro)
-- and getElementType ( carro ) == "vehicle"  then
--if getElementType(p2) == "player" then
--local vei = getPedOccupiedVehicle(p2)

--addEventHandler("onColShapeHit", psha, function(hit)
--if carro == "vehicle" then
addEventHandler("onVehicleEnter", root, function()
setTimer(function()
setPedAnimation(ped, false)
vei = getPedOccupiedVehicle(p)
warpPedIntoVehicle ( ped, root, math.random(2,4) ) 
end, 999, 1)
timers = getTimers ( 1000)
for timerKey, timerValue in ipairs(timers) do
      killTimer ( timerValue )
end
destroyElement(Marker)
--end
end)
end
--end
--end

I tried everything my :~ty skills allowed me to and now i'm out of ideas on how to solve this without Client-Side.

The idea is when the Ped approaches the car, it chooses between seats 2 or 3 and enters the car, but as i'm not creating the car in the script, instead you should be able to spawn any vehicle and as a Cop you could arrest people and take them to the car.

Sorry for the ugly-ass code.

 

EDIT:

Making the Ped follow the Player and when the Player enters a Vehicle the Ped is warped into the back seat is fine too.

Edited by Kraig Hellsing
Spelling mistakes that i was able to notice
Link to comment

I did some work on it by myself and here's what i've accomplished so far:

Server-Side

function reward(p)
if getElementType ( p ) == "ped" then
local carro = getVehicleOccupant(getPedOccupiedVehicle(p), 0)
givePlayerMoney(carro, 1000)

destroyElement(p)
outputChatBox("Perp has been turned in and you earned $1000!", carro, 0,255,0)

end
end

policem = createMarker(1586,-1677,5, "cylinder", 5, 255, 0, 0, 170)

addEventHandler("onMarkerHit", policem, reward)

function teste(p)
local x,y,z = getElementPosition(p)
local pedestre = createPed(23, x,y,z)
end
addCommandHandler("teste", teste)

team = createTeam("Criminal", 255,0,0)
team2 = createTeam("Police", 0,0,255)
--i play alone on my server, but it can easily be adapted to play with other players.


function findRotation3D( x1, y1, z1, x2, y2, z2 ) 
	local rotx = math.atan2 ( z2 - z1, getDistanceBetweenPoints2D ( x2,y2, x1,y1 ) )
	rotx = math.deg(rotx)
	local rotz = -math.deg( math.atan2( x2 - x1, y2 - y1 ) )
	rotz = rotz < 0 and rotz + 360 or rotz
	return rotx, 0,rotz
end

function func1(p)
local x,y,z = getElementPosition(p)
Marker = createMarker(x, y, z, "cylinder", 1, 255, 0, 0, 170)
attachElements(Marker,p, 0,1,0)
addEventHandler("onMarkerHit", Marker, bunda)
addEventHandler("onMarkerHit", Marker, cu)
end
addCommandHandler("bust", func1)

function release()
destroyElement(Marker)

timers = getTimers ( 1000)
for timerKey, timerValue in ipairs(timers) do
      killTimer ( timerValue )
end
end
addCommandHandler("free", release)


function func2(p,_)
if getElementType ( p ) == "ped" then
x2,y2,z2 = getElementPosition(Marker)
x1,y1,z1 = getElementPosition(p)

rx,ry,rz = findRotation3D(x1,y1,z1,x2,y2,z2)
setElementRotation(p, rx+0,ry+0,rz+0)
setPedAnimation(p, "ped", "walk_player")

setTimer(function()
func2(p,_)
end, 999, 1)
else return end
end


function func3(p, ped)
if not getElementType ( ped ) == "ped" then return end

addEventHandler("onVehicleEnter", root, function()
setTimer(function()
setPedAnimation(ped, false)

end, 999, 1)
timers = getTimers ( 1000)
for timerKey, timerValue in ipairs(timers) do
      killTimer ( timerValue )
end
destroyElement(Marker)

end)
end



function sirenes(p, pp)
 carro = getPedOccupiedVehicle(p)
if getVehicleSirensOn(carro) then

local x,y,z = getElementPosition(carro)
sir = createMarker(x,y,z, "cylinder", 5, 255,0,0, 170)
attachElements(sir,carro, 0,-2,0)

addEventHandler("onMarkerHit", sir, function(p)
if getElementType(p) == "ped" then
warpPedIntoVehicle(p, carro, math.random(2,3))
else return end
end)
else
destroyElement(sir)

end
end
addEventHandler("onVehicleStartExit", getRootElement(), sirenes)

function sirenoff(p, pp)
 carro = getPedOccupiedVehicle(p)
if getVehicleSirensOn(carro) then

destroyElement(sir)

end
end
addEventHandler("onVehicleEnter", getRootElement(), sirenoff)

 

Edited by Kraig Hellsing
Link to comment
  • Moderators
17 hours ago, Kraig Hellsing said:

I did some work on it by myself and here's what i've accomplished so far:

 

I cleaned up your first function a bit.

function reward(vehicle)
	if getElementType ( vehicle) == "vehicle" then
		local player = getVehicleOccupant(vehicle, 0)
		if player then
			givePlayerMoney(player, 1000)
			outputChatBox("Perp has been turned in and you earned $1000!", player, 0,255,0)
			
			destroyElement(source) -- destroy the marker, source of the event: addEventHandler("onMarkerHit", policem, reward)
		end
	end
end


function createPoliceMission ()
	local policem = createMarker(1586,-1677,5, "cylinder", 5, 255, 0, 0, 170)
	addEventHandler("onMarkerHit", policem, reward)
end

createPoliceMission ()

 

  • Like 1
Link to comment
16 hours ago, IIYAMA said:

 

I cleaned up your first function a bit.


function reward(vehicle)
	if getElementType ( vehicle) == "vehicle" then
		local player = getVehicleOccupant(vehicle, 0)
		if player then
			givePlayerMoney(player, 1000)
			outputChatBox("Perp has been turned in and you earned $1000!", player, 0,255,0)
			
			destroyElement(source) -- destroy the marker, source of the event: addEventHandler("onMarkerHit", policem, reward)
		end
	end
end


function createPoliceMission ()
	local policem = createMarker(1586,-1677,5, "cylinder", 5, 255, 0, 0, 170)
	addEventHandler("onMarkerHit", policem, reward)
end

createPoliceMission ()

 

thanks bro!

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