Jump to content

[Question]Is there a way to make function that will only fire once?


Rivskyy

Recommended Posts

Hey, I want to do a function witch will give a player a object (tire) and I want

to  give it only once (limit of tires in trunk). Is there a way to do it?

My script is serverside,I am begginer in scripting so dont hate me  :]

My script:

function bagaznik(player)
	vehicle2 = getPedOccupiedVehicle(player)
	if isElement(vehicle2) then
		if getVehicleDoorOpenRatio(vehicle2,1) > 0 then
			setVehicleDoorOpenRatio(vehicle2,1,0)
		else
			element = getPedOccupiedVehicle(player)
			 x,y,z = pozycja(element,0,-3,-1)
			 myMarker =createMarker ( x , y, z, "cylinder", 1.5, 255, 255, 0, 170 )
			addEventHandler("onMarkerHit", myMarker, sprawdzstan)
			setVehicleDoorOpenRatio(vehicle2,1,1)
		end
	end
	end
addCommandHandler("trunk",bagaznik)
--This funciton makes trunk open and create marker behind trunk and if you touch marker it run function sprawdzstan

function sprawdzstan(thePlayer)
if stan(thePlayer)==0 then
	 outputChatBox("#ffea00"..getVehicleName ( vehicle ).." jest sprawny: ",getRootElement(),r,g,b, true )
     outputChatBox ( "#ffea00Lewy-Przod: " .. stany [ lewyprzod ] .. "   #ffea00Prawy-Przod: " .. stany [ prawyprzod ]
      .."   #ffea00Lewy-tyl: " .. stany [ lewytyl ] .. "   #ffea00Prawy-Tyl: " .. stany [ prawytyl ],getRootElement(),r,g,b, true )
	  setVehicleDoorOpenRatio(vehicle,1,0)
	  destroyElement(myMarker)
else
	 outputChatBox("#ffea00"..getVehicleName ( vehicle ).."   jest zepsuty: ",getRootElement(),r,g,b, true )
	 outputChatBox ( "#ffea00Lewy-Przod: " .. stany [ lewyprzod ] .. "    #ffea00Prawy-Przod: " .. stany [ prawyprzod ]
      .. "   #ffea00Lewy-tyl: " .. stany [ lewytyl ] .. "   #ffea00Prawy-Tyl: " .. stany [ prawytyl ],getRootElement(),r,g,b, true  )
	 outputChatBox("By naprawić zepsute koło podejdz do pojazdu i odczekaj 5 sekund")
	 playanim(thePlayer)
end
end
--this function check if car wheels are damaged or not if not it output they are in good condition and destroy marker and close trunk
--if wheels are damaged it output they are damaged and run playanim

function  playanim(thePlayer)
		poprawka(thePlayer)
		setPedAnimation(thePlayer,"carry","crry_prtial",0,true,true,false,true)
		setTimer(function()setVehicleWheelStates ( vehicle, 0, 0, 0, 0 ) destroyElement(object) setPedAnimation(thePlayer,"carry","crry_prtial",0,true,true,false,false)
		destroyElement(myMarker) setVehicleDoorOpenRatio(vehicle,1,0)
		end,5000,1)
end
--this function set player animation to carry a object set on arms and after 5 seconds repair wheels in car destroy marker and set trunk close


function poprawka(thePlayer)
		x,y,z=getElementPosition(thePlayer )
		object =createObject (1327,x,y,z)
		setObjectScale(object,0.3)
		attachElements(object, thePlayer,-0.05,0.5,0.2,0,90,-100)
		setElementCollisionsEnabled(object, false)
end
-- function to set tire on player arms

function pozycja(element,offX,offY,offZ)
    local m = getElementMatrix ( element )  -- Get the matrix
    local x = offX * m[1][1] + offY * m[2][1] + offZ * m[3][1] + m[4][1]  -- Apply transform
    local y = offX * m[1][2] + offY * m[2][2] + offZ * m[3][2] + m[4][2]
    local z = offX * m[1][3] + offY * m[2][3] + offZ * m[3][3] + m[4][3]
    return x, y, z                               -- Return the transformed point
end
--function requried to get position of trunk

function getNearestVehicle( thePlayer )  
    local x, y, z = getElementPosition( thePlayer )   
    for i, v in ipairs( getElementsByType( "vehicle" ) ) do  
        local distance = getDistanceBetweenPoints3D( x, y, z, getElementPosition( v ) )  
		if distance <12 then 
            nearestVehicle = v 
        end  
    end 
    return nearestVehicle or false  
end 
--get distance and id etc. of the nearest car

function stan ( thePlayer)
	vehicle=getNearestVehicle( thePlayer )
	 stany = { [0]="#099e34Pełna", [1]="#FF0000Bez powietrza", [2]="#FF0000Odpadnięte koło" }
	 lewyprzod, lewytyl, prawyprzod, prawytyl = getVehicleWheelStates ( vehicle )
	 if lewyprzod==1 or lewytyl==1 or prawyprzod==1 or prawytyl==1 then
	 
	  else
	  
	  return lewyprzod, lewytyl, prawyprzod, prawytyl,stany
	end
end
--check car wheels condition that if is required to work sprawdzstan

--Script written by Rivsky :]

 

I want a limit in bagazanik function like:

Player open trunk and take's one tire,repair a tire and In second time when he puncture the tire and open trunk the output say the Trunk is empty and after get into the car car and get out of the car the trunk don't reset and will be still empty until I reset script.

If something is incomprehensible write here and I'll explain you and sorry If my English was not understandable :]

Link to comment
On 15/08/2019 at 15:08, WorthlessCynomys said:

setElementData()getElementData()

You should set the element data of the vehicle and chech that.

Like:


setElementData(veh, "trunkTires" 1);-- laterlocal tires = getElementData(veh, "trunkTires");if tires > 0 then  setElementData(veh, "trunkTires", tires-1);  -- Your get the tire from trunk codeelse  -- No tire in car, handle that tooend

 

Thank you for answer I will try it when I will have some  time

Link to comment

I tried with setElementData and getElementData  but it don't work.

I tried it like:

function bagaznik(player)
  	setElementData(vehicle2, "trunkTires",1)
	vehicle2 = getPedOccupiedVehicle(player)
	if isElement(vehicle2) then
		if getVehicleDoorOpenRatio(vehicle2,1) > 0 then
			setVehicleDoorOpenRatio(vehicle2,1,0)
		else
      	tires=getElementData(vehicle2, "trunkTires")
      	if tires>0 then
    		setElementData(vehicle2, "trunkTires",tires-1)
			element = getPedOccupiedVehicle(player)
			 x,y,z = pozycja(element,0,-3,-1)
			 myMarker =createMarker ( x , y, z, "cylinder", 1.5, 255, 255, 0, 170 )
			addEventHandler("onMarkerHit", myMarker, sprawdzstan)
			setVehicleDoorOpenRatio(vehicle2,1,1)
		end
	end
	end
addCommandHandler("trunk",bagaznik)

but getElementData havent update the value of tires left and it get only that 1 from the beggining,I suppose that reason of this problem is  that I take setElementData in function  and It's set 1 every time I open trunk,I tried getElementData from other function to prevent this situation but I don't know how to set new  value in other function becouse it will still get the first value.I will try other option like variable=1 and if variable>0 then... or with other options, if you have another idea to let it work let me know  and thank You for help :]

Link to comment

Try this

local markers = {}

function bagaznik(player)
	veh = getPedOccupiedVehicle(player)
	if (not isElement(veh) then
		return false
	end
	if getVehicleDoorOpenRatio(veh, 1) > 0 then
		setVehicleDoorOpenRatio(veh, 1, 0)
		if (markers[veh]) then
			removeEventHandler("onMarkerHit", markers[veh], sprawdzstan)
			destroyElement(markers[veh])
		end
		return true
	end
	tires = getElementData(veh, "trunkTires")
	if (tires) then
		return false
	end
	setElementData(veh, "trunkTires", true)
	x,y,z = pozycja(veh, 0, -3, -1)
	markers[veh] = createMarker (x, y, z, "cylinder", 1.5, 255, 255, 0, 170 )
	addEventHandler("onMarkerHit", markers[veh], sprawdzstan)
	setVehicleDoorOpenRatio(veh, 1, 1)
end
addCommandHandler("trunk",bagaznik)

What I have changed:

1- I made it so if you use /trunk then it will open trunk if it is closed and do create a marker (which I made it in a table instead of your variable so you can easily destroy your marker afterwards) and if you use /trunk and the trunk is opened, so it will close and destroy the marker (I assumed that you want to do that, if not just remove that line)

2- Changed your method of if statement to another one which is simpler to read.

  • Like 1
Link to comment

It works but It dont change the value when I for example hit pickup with attached setElementData with false,I would like to make this like

Tires is in trunk--->player repaired wheel-->no tire's in trunk-->player have to go on pickup or marker to get another new and loop from beggining.

Thank's for your help you gave me another few's solution's :]

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