Captain Cody Posted November 23, 2015 Share Posted November 23, 2015 (edited) Script is a trucking job, when player accepts job it creates blip and marker and connects trailer to the truck they are in, when they hit blip they get payed and the trailer disapears, Issue I'm having is when they hit the marker nothing happens, and none of the functions to remove trailer if they leave, disconnect etc are working. Cannot find what I did wrong. -- Detects truck and starts mission -- function startMissionAAA ( hitElement) if getElementType ( hitElement ) == "vehicle" and getVehicleOccupant ( hitElement ) --Creates trailer, and attaches it to the truck-- trailer = createVehicle (435, -208.95801, -211.38281, 1.421875, 0, 0, 180) setVehicleVariant (trailer, 0, 0) attachTrailerToVehicle ( hitElement, trailer ) Defines the driver and the truck -- PlayerAb = hitElementa truckMain = hitElement --Creates blips for the finnish-- finisha = createMarker (tableLocationsX[randomLoc], tableLocationsY[randomLoc], tableLocationsZ[randomLoc], "cylinder", 3, 0, 200, 55, 255, hitElementa) blip = createBlip (tableLocationsX[randomLoc], tableLocationsY[randomLoc], tableLocationsZ[randomLoc], 0, 2, 255, 0, 0, 255, 0, 10000, hitElementa) Event Handlers -- addEventHandler ("onTrailerDetach", getRootElement() , trailerDetach) addEventHandler ("onVehicleExit", getRootElement(), vehicleExit) addEventHandler ("onMarkerHit", finisha , EndTheMission) addEventHandler ("onPlayerQuit", getRootElement(), onQuit) Functions for removing trailer if something goes wrong -- function destroyVehicle () -- function trailerDetach () if source == trailer then -- function vehicleExit (PlayerAb) if source == truckMain then -- function onQuit () if source == PlayerAb then -- --End function -- function EndTheMission (hitElement) if hitElement == trailer then outputChatBox ("End money is " .. tableMoney[randomLoc] .. "$ If you would like to continue head back to one of the trucking depos.", PlayerAb, 255, 255, 255, true) givePlayerMoney (PlayerAb, tableMoney[randomLoc]) --destroyThings destroyElement (trailer) destroyElement (finisha) destroyElement (blip) --removeEventHandlers removeEventHandler ("onTrailerDetach", getRootElement(), trailerDetach) removeEventHandler ("onVehicleExit", getRootElement(), vehicleExit) removeEventHandler ("onPlayerQuit", getRootElement(), onQuit) else return false end end It may be alot but any help would be appreciated. I have been staying up till 3-4 am trying to get this script working. Edited November 26, 2015 by Guest Link to comment
Moderators IIYAMA Posted November 23, 2015 Moderators Share Posted November 23, 2015 You are doing that to yourself, if you are skipping the debug guide. https://wiki.multitheftauto.com/wiki/Debugging Every mistake can be found with that guide. Link to comment
Captain Cody Posted November 23, 2015 Author Share Posted November 23, 2015 What do you think I have been been doing? I wouldn't stay up till 3-5 am just to sit around and do nothing, I have been debugging it, I cannot find the issue. Link to comment
Moderators IIYAMA Posted November 23, 2015 Moderators Share Posted November 23, 2015 I am looking at your code and I can figure out some of the issues. Pls ask those questions bellow to yourself: - What happens when you execute the function getRootElement()? - What happens when you attach an addEventHandler with a root element? - What happens when you declare a global variable twice with a different value? For example the variable 'trailer'. Link to comment
Captain Cody Posted November 23, 2015 Author Share Posted November 23, 2015 Ohhh, thank you. Missed those, have been working on this script with no sleep for few days trying to get it working. Link to comment
TRtam Posted November 23, 2015 Share Posted November 23, 2015 Job basic(? : --<<< Important function startRe() --<<< The marker truckMr = createMarker(-709, 963, 12, "cylinder", 2) --<<< The blip truckBp = createBlip(-709, 963, 12, 51, 1) --<<< The event addEventHandler("onMarkerHit", truckMr, startMn) end addEventHandler("onResourceStart", getRootElement(), startRe) --<<< The Mision function startMn(hitElement) if getElementType(hitElement) == 'player' then --<<< Create the Truck truckM = createVehicle(514, -715, 963, 12) --<<< Attach the Trailer to the Vehicle setTimer(function() trailer = createVehicle ( 435, 0, 0, 4 ) -- create a trailer attachTrailerToVehicle ( truckM, trailer ) -- attach them end, 50, 1) --<<< Destroy the Marker of the Mission destroyElement(truckMr) outputChatBox("Go to the Waypoint") --<<< Create the Waypoint and the Marker BpMissionEnd = createBlip(-725, 874, 14, 41) MrMissionEnd = createMarker(-725, 874, 14, "cylinder") --<<< Create the event to finish the mission addEventHandler("onMarkerHit", MrMissionEnd, endMn) end end --<<< The end of the Mission function endMn(hitElement) if getElementType(hitElement) == 'player' then outputChatBox("You finished the Mission! Congratulations!") --<<< Destroy the other Markers and the Vehicle destroyElement(MrMissionEnd) destroyElement(BpMissionEnd) destroyElement(trailer) destroyElement(truckM) --<<< Give the money givePlayerMoney(hitElement, 1000) end end Link to comment
Captain Cody Posted November 23, 2015 Author Share Posted November 23, 2015 Well I already found the issue but something I cannot figure out is how to allow more then one person to do the job at a time, It creates more then one event handler for the players and over writes the first one. Any idea on how to get around this? Link to comment
Moderators IIYAMA Posted November 23, 2015 Moderators Share Posted November 23, 2015 For managing multiply data you need to use tables. For example you start a job for a player. truckerJobData = {} -- declare on top of your script. [/divbox] When you start this job, you do this: truckerJobData[player] = {} You use the player as key of the table. Which is between the brackets > [ ]. I saw tables before in your resource, so you probably already know how they work. You inset an empty table in there, which you can fill up later on. [divbox=#000000] The next step is to redeclare the table in a variable of choice: (this is just a method I like to use) local playerTruckerJobData = truckerJobData[player] [/divbox] After redeclaring the variable that gives access to the empty table, you are going to collect and create the data of the job. This can be the: truck, trailer, blip and marker. local finisha = createMarker (tableLocationsX[randomLoc], tableLocationsY[randomLoc], tableLocationsZ[randomLoc], "cylinder", 3, 0, 200, 55, 255, hitElementa) local blip = createBlip (tableLocationsX[randomLoc], tableLocationsY[randomLoc], tableLocationsZ[randomLoc], 0, 2, 255, 0, 0, 255, 0, 10000, hitElementa) local truckM = createVehicle(514, -715, 963, 12) local trailer = createVehicle ( 435, 0, 0, 4 ) [divbox=#000000] And the last step is to insert the required data of the job, inside your empty table. playerTruckerJobData["blip"] = blip playerTruckerJobData["marker"] = finisha playerTruckerJobData["truck"] = truckM playerTruckerJobData["trailer"] = trailer There are a few ways to do it, but try first this one, because it is easy to read. [/divbox] Ones you did that, you can access your: trailers, trucks, blips and markers very easy. The only thing you need to do is knowing the player/(player-userdata). To access your trailer and destroy it, example: local playerTruckerJobData = truckerJobData[player] if playerTruckerJobData then -- if the player have any job data then ... local trailer = playerTruckerJobData["trailer"] -- then get the trailer element. if isElement(trailer) then -- check if the trailer still does exist in the MTA world. destroyElement(trailer) -- destroy the trailer. end end Link to comment
Captain Cody Posted November 23, 2015 Author Share Posted November 23, 2015 The issue is not the blips and stuff, those individually get assigned to the player who hit the marker. But the functions that let the player actually finish the mission, and remove the trailer and finish point if the player leaves the truck or disconnects. Link to comment
Moderators IIYAMA Posted November 23, 2015 Moderators Share Posted November 23, 2015 (edited) I am looking at your code and I can figure out some of the issues. Pls ask those questions bellow to yourself:- What happens when you execute the function getRootElement()? - What happens when you attach an addEventHandler with a root element? - What happens when you declare a global variable twice with a different value? For example the variable 'trailer'. Ask the last question again to yourself. Isn't it obviously with a job resource that more players are using the same variables? If you can't and won't understand the logic of this, then you will be working on a system that can never be used by multiple players. I can't fix something that can't be fixed, sorry. Edited November 24, 2015 by Guest Link to comment
Captain Cody Posted November 23, 2015 Author Share Posted November 23, 2015 How would 2 or more players use the same function? When one player gets they trailer/load it over writes the previous player and will not let them deliver. Link to comment
Moderators IIYAMA Posted November 24, 2015 Moderators Share Posted November 24, 2015 because there is only 1 function or well there is only 1 variable that holds that function. But that is not the problem, the problem is with the data. The data is shared between all other players, because it is serverside code. Link to comment
Captain Cody Posted November 24, 2015 Author Share Posted November 24, 2015 I know that I cannot create more then one of the same functions, but the question I'm asking is how would I get it so 2 or more people can use the same function in this just Example : "Finisha" if one person accepts the job then another person accepts it, then it removes the first players ability to finish the job. How would I get around this? Link to comment
Moderators IIYAMA Posted November 24, 2015 Moderators Share Posted November 24, 2015 If you want to get around that, you will have to assign this job to only player. Doing this by storing it's userdata/player inside a variable and checking it when another player tries to do the same job. Link to comment
Captain Cody Posted November 24, 2015 Author Share Posted November 24, 2015 The question is, How would I do this with EventHandlers? addEventHandler ("onVehicleExit", rootElement, vehicleExit) addEventHandler ("onMarkerHit", FinishPoint , EndTheMission) addEventHandler ("onPlayerQuit", rootElement, onQuit) Link to comment
Moderators IIYAMA Posted November 24, 2015 Moderators Share Posted November 24, 2015 onVehicleExit, replace the rootElement with the truck element. onPlayerQuit, replace the rootElement with the player element. And with onMarkerHit you compare inside the function, the hitElement(first argument) with the truck element. Link to comment
Captain Cody Posted November 24, 2015 Author Share Posted November 24, 2015 Ok I thought I had got it working but can you explain more clearly what And with onMarkerHit you compare inside the function, the hitElement(first argument) with the truck element. Means? function EndTheMission (hitElement) if hitElement == TrailerAb then And the event handler for it - addEventHandler ("onMarkerHit", FinishPoint , EndTheMission) Here is what I got, it works for the first person, then when a secound person tries to deliver it does not work. Link to comment
Moderators IIYAMA Posted November 24, 2015 Moderators Share Posted November 24, 2015 This is exactly what I meant. function EndTheMission (hitElement) if hitElement == TrailerAb then Here is what I got, it works for the first person, then when a secound person tries to deliver it does not work. As I said before, it makes the resource only working for 1 player at the time. When you want more players to be able to use it at the same time, you simply need tables. Link to comment
Captain Cody Posted November 25, 2015 Author Share Posted November 25, 2015 for the table, how would I make it so a trailer created on marker hit is part of it? Link to comment
Saml1er Posted November 25, 2015 Share Posted November 25, 2015 Here's a simple example: local data = { } -- define a table function EndTheMission (hitElement) if hitElement == TrailerAb then data [ hitElement ] = "Hey "..getPlayerName(hitElement) -- insert it in table. end end addEventHandler ("onMarkerHit", FinishPoint , EndTheMission) addCommandHandler ("a", function (p ) local v =data[p] if v then outputChatBox ( v ) end end ) When you hit the marker than a string is inserted and index is a player. If you want to see the output then execute command /a Please kindly google "lua tables" and learn how they work for better explanation. Link to comment
Captain Cody Posted November 25, 2015 Author Share Posted November 25, 2015 Well I know how to make table and stuff, but the trailer is created when the player hits a marker. function startMissionAAA ( hitElement) if getElementType ( hitElement ) == "vehicle" and getVehicleOccupant ( hitElement ) then --Trailer location-- TrailerAb = createVehicle (435, TrailerX, TrailerY, TrailerZ, 0, TrailerRotation, trailerType[randomLoc]) setVehicleVariant (TrailerAb, 5, 0) attachTrailerToVehicle ( hitElement, TrailerAb ) I'm wondering how I would add the trailer created here into the table. Link to comment
Moderators IIYAMA Posted November 25, 2015 Moderators Share Posted November 25, 2015 for the table, how would I make it so a trailer created on marker hit is part of it? In a post before I explained exacly how to store and receive data from a table. Link to comment
Captain Cody Posted November 25, 2015 Author Share Posted November 25, 2015 Probably did 90% of this wrong. But here's what I have -- For start mission - function startMissionAAA ( hitElement) if getElementType ( hitElement ) == "vehicle" and getVehicleOccupant ( hitElement ) then PlayerAb = getVehicleOccupant ( hitElement ) truckerJobData[PlayerAb] = {} local playerTruckerJobData = truckerJobData[PlayerAb] local trailer = createVehicle (435, TrailerX, TrailerY, TrailerZ, 0, TrailerRotation, trailerType[randomLoc]) setVehicleVariant (TrailerAb, 5, 0) attachTrailerToVehicle ( hitElement, TrailerAb ) setPlayerTeam ( PlayerAb, TruckDriverAA ) truckMain = hitElement --Location stuff-- destroyElement (markerAAB) randomLoc = math.random (1, locationCount) local FinishPoint = createMarker (tableLocationsX[randomLoc], tableLocationsY[randomLoc], tableLocationsZ[randomLoc], "cylinder", 3, 0, 200, 55, 255, PlayerAb) local blip = createBlip (tableLocationsX[randomLoc], tableLocationsY[randomLoc], tableLocationsZ[randomLoc], 0, 2, 255, 0, 0, 255, 0, 10000, PlayerAb) --Chat Stuff-- outputChatBox ("Drive to the red blip on the map", PlayerAb, 255, 255, 255, true) --addEventHandlers addEventHandler ("onVehicleExit", truckMain, vehicleExit) addEventHandler ("onMarkerHit", FinishPoint , EndTheMission) addEventHandler ("onPlayerQuit", PlayerAb, onQuit) --Add the stuff into table-- playerTruckerJobData["blip"] = blip playerTruckerJobData["marker"] = FinishPoint playerTruckerJobData["trailer"] = trailer else return false end end and for end mission -- function EndTheMission () local playerTruckerJobData = truckerJobData[PlayerAb] if playerTruckerJobData then local TrailerAb = playerTruckerJobData["trailer"] local FinishPoint = playerTruckerJobData["marker"] local blip = playerTruckerJobData["blip"] if getElementType ( hitElement ) == "trailer" then outputDebugString("EndTheMission") outputChatBox ("This load got you " .. tableMoney[randomLoc] .. "$ If you would like to continue head back to one of the trucking depos.", PlayerAb, 255, 255, 255, true) givePlayerMoney (PlayerAb, tableMoney[randomLoc]) setPlayerTeam ( PlayerAb, NoJob ) --destroyThings destroyElement (TrailerAb) destroyElement (FinishPoint) destroyElement (blip) --removeEventHandlers removeEventHandler ("onVehicleExit", truckMain, vehicleExit) removeEventHandler ("onPlayerQuit", PlayerAb, onQuit) else return false end end Gives me error, end expected near 188 "The end line, right after the end mission function" addEventHandler ("onResourceStart", getResourceRootElement(getThisResource()), startScript) Link to comment
Moderators IIYAMA Posted November 25, 2015 Moderators Share Posted November 25, 2015 startMissionAAA < execute this function with the player as first argument. Don't add an addEventHandler on this function, but call it from another function(which might have an addEventHandler). function startMissionAAA (playerAb) local truckMain = getPedOccupiedVehicle(playerAb) if truckMain then truckerJobData[playerAb] = {} local playerTruckerJobData = truckerJobData[playerAb] local trailer = createVehicle (435, TrailerX, TrailerY, TrailerZ, 0, TrailerRotation, trailerType[randomLoc]) setVehicleVariant (TrailerAb, 5, 0) attachTrailerToVehicle ( truckMain, TrailerAb ) setPlayerTeam ( playerAb, TruckDriverAA ) --Location stuff-- local randomLoc = math.random (1, locationCount) local finishPoint = createMarker (tableLocationsX[randomLoc], tableLocationsY[randomLoc], tableLocationsZ[randomLoc], "cylinder", 3, 0, 200, 55, 255, playerAb) local blip = createBlip (tableLocationsX[randomLoc], tableLocationsY[randomLoc], tableLocationsZ[randomLoc], 0, 2, 255, 0, 0, 255, 0, 10000, playerAb) --Chat Stuff-- outputChatBox ("Drive to the red blip on the map", playerAb, 255, 255, 255, true) --addEventHandlers addEventHandler ("onVehicleExit", truckMain, vehicleExit) addEventHandler ("onMarkerHit", finishPoint , endTheMission) addEventHandler ("onPlayerQuit", playerAb, onQuit) --Add the stuff into table-- playerTruckerJobData["vehicle"] = truckMain playerTruckerJobData["blip"] = blip playerTruckerJobData["marker"] = finishPoint playerTruckerJobData["trailer"] = trailer else return false end end function endTheMission (truckMain,matchingDimension) if matchingDimension then local playerAb = getVehicleOccupant(truckMain) if playerAb then local playerTruckerJobData = truckerJobData[playerAb] if playerTruckerJobData and playerTruckerJobData["vehicle"] == truckMain and playerTruckerJobData["marker"] == source then -- Check if all data is correct. outputDebugString("endTheMission") local randomMoney = (tonumber(tableMoney[randomLoc]) or 0) outputChatBox ("This load got you " .. randomMoney .. "$ If you would like to continue head back to one of the trucking depos.", playerAb, 255, 255, 255, true) givePlayerMoney (playerAb, randomMoney) setPlayerTeam ( playerAb, NoJob ) ------------------ -- the clean up -- local trailerAb = playerTruckerJobData["trailer"] local finishPoint = playerTruckerJobData["marker"] local blip = playerTruckerJobData["blip"] --removeEventHandlers removeEventHandler ("onVehicleExit", truckMain, vehicleExit) removeEventHandler ("onPlayerQuit", playerAb, onQuit) removeEventHandler ("onMarkerHit", source , endTheMission) --destroyThings if isElement(trailerAb) then -- validate(if is still exist) before destroy destroyElement (trailerAb) end if isElement(finishPoint) then -- validate(if is still exist) before destroy destroyElement (finishPoint) end if isElement(blip) then -- validate(if is still exist) before destroy destroyElement (blip) end destroyElement(source) -- destroy the marker, no validate needed. (element used in event, so must exist) destroyElement(truckMain) -- destroy the truck, no validate needed. (element used in event, so must exist) truckerJobData[playerAb] = nil -- clean up -- the clean up -- ------------------ end end end end Link to comment
Captain Cody Posted November 25, 2015 Author Share Posted November 25, 2015 That worked but I'm getting debug errors, for the on quit or on truck leave, expected function at argument 3, got null. Functions ----- function destroyVehicle () if destroyTimer then --destroyThings local playerTruckerJobData = truckerJobData[PlayerAb] if playerTruckerJobData then local TrailerAb = playerTruckerJobData["trailer"] local FinishPoint = playerTruckerJobData["marker"] local blip = playerTruckerJobData["blip"] destroyElement (TrailerAb) destroyElement (FinishPoint) destroyElement (blip) --removeEventHandlers removeEventHandler ("onVehicleExit", truckMain, vehicleExit) removeEventHandler ("onVehicleEnter", truckMain, destrTimer) removeEventHandler ("onPlayerQuit", PlayerAb, onQuit) else return false end end function vehicleExit (PlayerAb) if source == truckMain then outputChatBox ("Go back to the truck within a minute", PlayerAb, 255, 255, 255, true) destroyTimer = setTimer (destroyVehicle, 600000, 1) addEventHandler ("onVehicleEnter", truckMain, destrTimer) else return false end end function destrTimer (thePlayer) if source == truckMain and thePlayer == playerAb then local team = getPlayerTeam (thePlayer) if getTeamName (team) == "Other" or getTeamName (team) == "Police" then return false else killTimer (destroyTimer) removeEventHandler ("onVehicleEnter", truckMain, destrTimer) setVehicleLocked (truckMain, true) end else return false end end function onQuit () if source == PlayerAb then --destroyThings local playerTruckerJobData = truckerJobData[PlayerAb] if playerTruckerJobData then local TrailerAb = playerTruckerJobData["trailer"] local FinishPoint = playerTruckerJobData["marker"] local blip = playerTruckerJobData["blip"] destroyElement (TrailerAb) destroyElement (FinishPoint) destroyElement (blip) --removeEventHandlers removeEventHandler ("onVehicleExit", truckMain, vehicleExit) removeEventHandler ("onPlayerQuit", PlayerAb, onQuit) end end end end --- The event handlers --- On mission start -- addEventHandler ("onVehicleExit", truckMain, vehicleExit) addEventHandler ("onPlayerQuit", playerAb, onQuit) On mission end -- removeEventHandler ("onVehicleExit", truckMain, vehicleExit) removeEventHandler ("onPlayerQuit", playerAb, onQuit) Link to comment
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now