whiskeyzulu Posted April 21, 2016 Share Posted April 21, 2016 can any of you brainics tell me why this script doesn't give me the coordinates of a random marker?? local markers = getElementsByType("marker") local x,y,z = getElementPosition(markers[math.random(#markers)]) debug tells me that the argument passed to random is nil. There are definitely 27 markers on the map (created with map editor). Maybe I am calling getElementsByType before the map objects have loaded?? Link to comment
Walid Posted April 21, 2016 Share Posted April 21, 2016 It's a table all what you need is for i , v in pairs ( markers) do local x,y,z = getElementPosition(v) -- You code here end Link to comment
whiskeyzulu Posted April 21, 2016 Author Share Posted April 21, 2016 that would give me the coordinates of each marker. It still doesn't solve how to pick one of those markers to spawn at or whatever. I solved the problem. I included a minimum random argument along with a maximum argument. I believe my original code above would sometimes randomly throw out a zero and cause the code to break. I replaced the code with this: local markers = getElementsByType("marker") local x,y,z = getElementPosition(markers[math.random(1,#markers)]) all is fine! I randomly spawn at any one of my 27 markers! Thanks for trying to help. Big love. Link to comment
whiskeyzulu Posted April 21, 2016 Author Share Posted April 21, 2016 spoke too soon! Problem still happens sporadically. #markers is sometimes throwing out nil when I restart my resource. Link to comment
Walid Posted April 21, 2016 Share Posted April 21, 2016 spoke too soon! Problem still happens sporadically. #markers is sometimes throwing out nil when I restart my resource. if markers then end Link to comment
whiskeyzulu Posted April 21, 2016 Author Share Posted April 21, 2016 so I should enter a while loop until "markers" are true then build the table. Why are the markers I have placed on the map in the map editor not showing up when i start my server? I had this working yesterday!! LOL Link to comment
whiskeyzulu Posted April 21, 2016 Author Share Posted April 21, 2016 It appears that my problem was due to the fact that I was loading an old version of my map file that did not have any markers placed inside it! I need to work on my version control. My function now works properly and reliably. It is a bit clunkier than I would like so any tips or comments as to how I may be able to clean it up a little would be most greatly appreciated. I still couldn't access a table of markers by indexing directly with a math.random call within square brackets (i.e. randomMarker = marker[math.random(1,#marker)]). I ended up using a for loop to iterate through ipairs as usual. function warper() local player = getPlayerName(source) local vehicleID = 0 local x,y,z = 0 if player == "pilot" then --pilot gets a police maverick helicopter vehicleID = 497 elseif player == "patrol1" or player == "patrol2" then --patrolmen get a police car vehicleID = 596 else --bandits get a taxi vehicleID = 438 end local markers = getElementsByType("marker") local randomIndex = math.random(1,#markers) for i, marker in ipairs (markers) do if player == "pilot" and i == 26 then x,y,z = getElementPosition(marker) elseif player ~= "pilot" and i == randomIndex then x,y,z = getElementPosition(marker) end --make all markers invisible setElementVisibleTo(marker,root,false) end z = z + 0.5 --make sure vehicle is created above ground local newVehicle = createVehicle ( vehicleID, x, y, z) --only bandits can be damaged if player ~= "bandit" then setVehicleDamageProof(newVehicle,true) end spawnPlayer(source,x,y,z+20) warpPedIntoVehicle(source,newVehicle) fadeCamera(source,true) end addEvent("onWarp",true) addEventHandler("onWarp",getRootElement(),warper) addEventHandler("onPlayerJoin",getRootElement(),warper) Link to comment
Anubhav Posted April 22, 2016 Share Posted April 22, 2016 I guess: Server files load first, so the markers may not be created at first. You may want to use loadMapData at starting of the resource. 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