Jump to content

Need help with functions


Crook4Money

Recommended Posts

I'm basically trying to tell one function that if another function happens to go ahead and do the thing. 

function moveDoor(player, vehicle) 
	local vehicle = getPedOccupiedVehicle(player)
	local money = getPlayerMoney(player)	
    if ( money >= 200 ) and getElementHealth(vehicle) < 1000 then
	if isMoving then return end
	if source == marker1 then
		local move = moveObject(door1, moveTime, 2071.5, -1831.4000244141, 14.60000038147, 0, 90, 0)
		triggerClientEvent("doorSound", resourceRoot)
		if move then 
			isMoving = false
			setTimer(moveDoor, moveTime, 1, true)
		end
	end
end
end
addEventHandler("onMarkerHit", marker1, moveDoor)

function door2(player, vehicle)
	setTimer(function() 
    if moveDoor = true then
	moveObject(door1, moveTime, 2072.5, -1831.4200244141, 16.5, 0, -90, 0)
		isMoving = false
		triggerClientEvent("doorSound", resourceRoot)
end
end, 6000, 1)
end
addEventHandler("onMarkerHit", marker1, door2)

I'm trying to do this as the second door function is giving me problems so bypassing the whole "trying to get it to work similarly" would be great if I could just tell it to start if the first door function starts. My problem with duplicating the scripts which wasn't a problem when I first did it, but then became one is that it says that I'm trying to attach numbers to booleans, nils or even that the player element is registering as a vehicle. 

Edited by Crook4Money
Link to comment

Honestly, you're trying to complicate it, start small, work your way up....  Make the marker first, then pass the parameters that onMarkerHit returns in the brackets of function, the way you have done this is not right...  But I don't know if you missed it or just didn't understand the returns on the wiki, so I will explain.

Always read this stuff, don't just muck with code and hope it works, I learned this the hard way....

Parameters

 
  1. element hitElement, bool matchingDimension
  • hitElement: The element that hit the marker
  • matchingDimension: True if the element is in the same dimension as the marker he hit

Source

The source of this event is the marker that got hit by the element.

 

So the parameters for example would be "function(hitElement, matchingDimension)" these return what the wiki say, but they are not a "special phrase", "special word" or "magic word", meaning you can call these 2 almost anything you want, a "magic word" would be source, this will return the source element, so "theMarker = source" in your first function would be the marker, as that is the source element, the dimension is true if the marker and element you are checking is in the same dimension, otherwise they are false, not there.... 

You'll want to check the element, in case it's a spawned ped...  So I'm just going to re-type your code at a level I think you should start at, then I want you to go to wiki, and read a little about it so you know what's happening.

 

isMoving = false --You need to put these out of your functions if you want to use them across multiple functions, later in lua scripting once you are at a more advanced level you can learn how to pass them back and forth and use subroutines
moveTime = 5*1000 --Set the number to the time you want it to move, we MUST use miliseconds, so 5*1000 = 5 seconds
--Add a marker with createMarker

function moveDoor(hitElement, dimension) --We DON'T put the dimension as vehicle here as you are setting vehicle to the players vehicle below
	local vehicle = getPedOccupiedVehicle(hitElement) --We use hitElement as it is passed from event handler
	local money = getPlayerMoney(hitElement)	
    if ( money >= 200 ) and getElementHealth(vehicle) < 1000 then
		if isMoving == true then 
    		return 
    	end 
		if source == marker1 then
			moveObject(door1, moveTime, 2071.5, -1831.4000244141, 14.60000038147, 0, 90, 0)
			triggerClientEvent("doorSound", resourceRoot) --I'm assuming this works, I would not start sounds if you are just starting
			isMoving = true --We don't check move, moveObject doesn't wait, so best bet is to just set it true, set a timer to set it false
			--setTimer(moveDoor, moveTime, 1, true) --DO NOT DO THIS, YOU WILL LOOP INFINITELY AS YOU CALL THE FUNCTION INSIDE ITSELF.
			setTimer(door2, moveTime, 1, true)--This will trigger door2 after moveTime is over
    	end
	end
end
addEventHandler("onMarkerHit", marker1, moveDoor)

function door2(player, vehicle) --You can try figure out what's going wrong here yourself, look at the above and see what's been changed
	setTimer(function() 
    if moveDoor = true then
	moveObject(door1, moveTime, 2072.5, -1831.4200244141, 16.5, 0, -90, 0)
		isMoving = false
		triggerClientEvent("doorSound", resourceRoot)
end
end, 6000, 1)
end
addEventHandler("onMarkerHit", marker1, door2)

Use /debugscript 3 when testing scripts, carefully read the wiki, look at the examples, look at the parameters, look at what the full page says and not just a little of the page...  I could do it all for you, but then you'd maybe learn nothing, so just try your hardest with what I've gave you and if you really are stuck, I'll try explain and help until you understand it. :D 

Edited by kieran
Link to comment
2 minutes ago, Crook4Money said:

That was just the part of the script I was having issues with. The entire script is 81 lines.

Ah, I see, well none the less, the way you used vehicle is wrong, check it again and remember that the wiki is there for a reason :P it's very useful.

Link to comment

I use the wiki everytime I have a problem. I guess I just messed up the vehicle part as I was copying it from the beginning of my script that does something entirely different.

function moveDoor(hitElement, dimension)

I'm also completely oblivious to the use of the words in the ( ) as well which has been causing me issues for quite some time.

Link to comment
2 minutes ago, Crook4Money said:

I use the wiki everytime I have a problem. I guess I just messed up the vehicle part as I was copying it from the beginning of my script that does something entirely different.


function moveDoor(hitElement, dimension)

I'm also completely oblivious to the use of the words in the ( ) as well which has been causing me issues for quite some time.

It is hard to put it in words when you have learned from just trying stuff, but I'll try explain why words are in ( ) in your function...

See the "Parameters" on the wiki?  These are the values that your event handlers return, so if it returns 2 parameters, then your first 2 words in the brackets will be the parameters (the stuff that the function is returning, such as the player, and the dimension) it's not the easiest to understand, but once you understand it you'll get it, I recommend you look up passing parameters and also using subroutines in lua, a subroutine/subprogram is basically a function that has 2 values, you can then call another function if you added a custom event with addEvent and addEventHandler, to pass these parameters to a custom event you use for example... 

function login()
	triggerClientEvent("myEvent", source, 20, 30, "hello") 
end

addEventHandler("onPlayerLogin", getRootElement(), login)

So the above is The event (attached to the function you want to pass stuff to) and the source, so if it was onPlayerLogin, you'd pass the player as the source element, anything after that is stuff to be passed to your function.

So for client side code you can do something like the following using the server side code above.

function passedOnLogin(num1,num2,text)
	outputChatBox(""..text,tonumber(num1), 0, tonumber(num2))
end

addEvent("myEvent", true)
addEventHandler("myEvent", root, passedOnLogin)

triggerClientEvent("myEvent", source, 20, 30, "hello") 

This would output the message "hello" when the player logs in by passing it to the client, it would also change color of the text to 20, 0, 30 (rgb)

Hope this helps :)

Link to comment

Unfortunately the changes made have the same result. The second door still isn't responding correctly. It's actually responding the exact same way as it was previously. This was the change I needed to work. I don't know how to get it to respond to the second line of script as I was actually asking this question originally.

setTimer(door2, moveTime, 1, true)--This will trigger door2 after moveTime is over

Edit: Before this post door 1 was responding correctly and door 2 was either responding incorrectly or not at all. After the changes this is still the case.

Edited by Crook4Money
Link to comment

You're not being clear in the result you want, do you want the doors to open together?  If so why don't you just move both doors in the same script with moveObject?

Also line numbers and some debugscript 3 outputs would help, so I know what problems you're having if there is any...  I'm a guy not a mind reader.

Edited by kieran
Link to comment

Oh, then just set a global boolean (something that's true or false) before your functions and check it....

--add some variables/booleans, to check what door is open
door1open = false
door2open = false

function moveDoor(hitElement, dimension) 
	local vehicle = getPedOccupiedVehicle(hitElement) 
	local money = getPlayerMoney(hitElement)	
	if ( money >= 200 ) and getElementHealth(vehicle) < 1000 then
		if door1open == false then --if door 1 is not open we open it and set it to true
			moveObject(door1, moveTime, 2072.5, -1831.4200244141, 16.5, 0, -90, 0)
			door1open = true
			triggerClientEvent("doorSound", resourceRoot)
		else
			--Move the door back here
		end
		if door1open == true and door2open == false then --If door 1 is open and door 2 is not (player will need to enter marker again)
      		--Code to open second door
		else
      		--code to close it
		end
	end
end

addEventHandler("onMarkerHit", marker1, moveDoor)

If you mean open door1 then open door2 after, why don't you just move them in the same if checking if they are closed (if doorsOpen == false) and set your timer on the second door to the "moveTime" ...  This would open your door after the first door has opened... 

Edited by kieran
Link to comment

I had to try about a couple hundred different variables to get what I wanted, but I finally got it using my previous script. 

function moveDoor(player, vehicle) 
	local vehicle = getPedOccupiedVehicle(player)
	local money = getPlayerMoney(player)	
    if ( money >= 200 ) and getElementHealth(vehicle) < 1000 then
		local move = moveObject(door1, moveTime, 2071.5, -1831.4000244141, 14.60000038147, 0, 90, 0)
		triggerClientEvent("doorSound", resourceRoot)
		if move then 
			isMoving = false
			setTimer(moveDoor, moveTime, 1, true)
			end
			setTimer(function()
		moveObject(door1, moveTime, 2072.5, -1831.4200244141, 16.5, 0, -90, 0)
		isMoving = false
		triggerClientEvent("doorSound", resourceRoot)
		end, 6000, 1)
	end
end
addEventHandler("onMarkerHit", marker1, moveDoor)

Function thing may be wrong again, but whatever. It works. 

  • Thanks 1
Link to comment
47 minutes ago, Crook4Money said:

I just didn't know you could set a separate timer within a single function. I tried it a couple times before, but it didn't work for whatever reason. Maybe my pointless return values were messing something up. 

When you return you basically end it, but you can use return to return values to a function, if it doesn't work...  You just think of it like function nesting...  setTimer = (function() Contents of function, end, time, times to repeat, other values) :D

Or to set a timer for a function... 

setTimer = (function name, Contents of function, end, time, times to repeat, other values )

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