Baseplate Posted February 14, 2013 Posted February 14, 2013 robbing = {} tehMarker = createMarker(x, y, z, "cylinder") function MarkerHit( hitElement, matchingDimension ) if getElementType( hitElement ) == "player" then table.insert(robbing, hitElement) end end addEventHandler( "onMarkerHit", myMarker, MarkerHit ) function markerLeave( leaveElement, matchingDimension ) if getElementType( leaveElement ) == "player" then table.remove(robbing, leaveElement) end end addEventHandler( "onMarkerLeave", myMarker, markerLeave ) Will this work and how to get players from this table?
csiguusz Posted February 14, 2013 Posted February 14, 2013 With table.remove you can remove elements from tabke by index: function markerLeave( leaveElement, matchingDimension ) if getElementType( leaveElement ) == "player" then for i, v in ipairs( robbing ) do if v == leaveElement then table.remove( robbing, i ) end end end end And what do you mean by getting players from the table? Use a for loop for example... Owner of [HUN]Magyar Play Szerver 1.4, IP: 31.220.43.153:22003
Baseplate Posted February 15, 2013 Author Posted February 15, 2013 I meant, I wanted to use the players who's in this table to do something else.
Moderators IIYAMA Posted February 15, 2013 Moderators Posted February 15, 2013 Then you also have to put the table in pairs. for i, v in ipairs( robbing ) do givePlayerMoney (v, 20000) end Do you want to improve your Lua programming skills and make less mistakes? Start with Lua Language Server! Useful functions 3x Spoiler checkPassiveTimer getScreenStartPositionFromBox getPedGender Tutorials 4x Spoiler Scaling DX Events Attach an addEventHandler on a group of elements Debugging
Baseplate Posted February 15, 2013 Author Posted February 15, 2013 Aha, thanks! Another question, how to count how much elements/players are in this table?
Baseplate Posted February 15, 2013 Author Posted February 15, 2013 Again a new question, anyway to count how much players in that marker?
TAPL Posted February 15, 2013 Posted February 15, 2013 Not tested, and not sure if it will work or not. function getPlayersCountInMarker(marker) if isElement(marker) and getElementType(marker) == "marker" then local col = getElementColShape(marker) if col then local count = getElementsWithinColShape(col, "player") if count and type(count) == "table" then return #count end end end end
Moderators IIYAMA Posted February 15, 2013 Moderators Posted February 15, 2013 -- not sure when a player leavesthe server, the onMarkerLeave will be triggered. local inside = {} function MarkerHit( hitElement, matchingDimension ) if getElementType( hitElement, ) == "player" then table.insert (inside,{hitElement,source}) end end addEventHandler( "onMarkerHit", myMarker, MarkerHit ) function markerLeave( leaveElement, matchingDimension ) if getElementType( leaveElement, ) == "player" then for i, v in ipairs( inside ) do if v[2] == leaveElement then table.remove( inside, i ) --v[1]) player end end end end addEventHandler( "onMarkerLeave", myMarker, markerLeave ) --#inside this shows you how many people are inside -- source is the marker. -- btw not sure if it works 100%, I made it in a hurry.... The structure of this table will look like this: local inside = { {player,marker}, --<<< {player,marker}, {player,marker}, {player,marker} } v = inside[1] -- take the first position <<< player,marker = unpack (v) -- or player = v[1] marker = v[2] Do you want to improve your Lua programming skills and make less mistakes? Start with Lua Language Server! Useful functions 3x Spoiler checkPassiveTimer getScreenStartPositionFromBox getPedGender Tutorials 4x Spoiler Scaling DX Events Attach an addEventHandler on a group of elements Debugging
Anderl Posted February 15, 2013 Posted February 15, 2013 I recommend you using iteration statements to find the length of an array, since the symbol '#' does not count anything else than number indexes. Just in case you don't know what an iteration statement is: it's a for loop. "[...] If you don’t love it, if you’re not having fun doing it, you don’t really love it, you’re going to give up." - Steve Jobs, 2007
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