Jump to content

Ehm, loop failing?


Recommended Posts

Only first 2 outputs are done, the other 2 arent given every sec.. (I've already placed the 2 markers and see them)

  
  
function MarkerCheckMed() 
outputChatBox("Checking Distance", getRootElement(), 255,255,255,true) 
for i,v in ipairs(getElementsByType("player")) do 
outputChatBox("Checking player "..getPlayerNametagText(v), getRootElement(), 255,255,255,true) 
for s, k in ipairs(MedKit1) do 
outputChatBox("Checking Player Marker ", getRootElement(), 255,255,255,true) 
if getDistanceBetweenPoints3D(k, v) <= 15 then 
outputChatBox("Distance Met!", getRootElement(), 255,255,255,true) 
if not getElementData(v, "Heal") then 
setElementData(v, "Heal", true) 
end 
else 
if getElementData(v, "Heal") then 
removeElementData(v, "Heal") 
end 
end 
end 
end 
end 
MarkerTim = setTimer(function() 
MarkerCheckMed() 
end, 1000, 0) 
  
MedKit1 = {} 
MedKit2 = {} 
function MedKits(player,cmd) 
if getElementData(player,"MediKits") > 0 then 
meds = getElementData(player, "MediKits") 
local x,y,z = getElementPosition(player) 
setElementData(player, "MediKits", meds - 1) 
if MedKit1[player] then 
MedKit2[player] = createMarker(x,y,z,"corona", 1, 0,255,0,250, getPlayersInTeam(getPlayerTeam(player))) 
else 
MedKit1[player] = createMarker(x,y,z,"corona", 1, 0,255,0,250, getPlayersInTeam(getPlayerTeam(player))) 
end 
end 
end 
addCommandHandler("MedKit", MedKits, false,false) 

Link to comment
Found the problem, you are using ipairs, but your table isn't a numerical indexed one.
for s, k in pairs ( MedKit1 ) do 

How could i index it correctly in this case? cant find a way arround..

EDIT: I know i can do table.insert, but how i assing a key to that value i inserted? Lets say i insert variable K, How i give K a value? (in this case CreateMarker)

Link to comment
There's no need to do it, that loop will do just fine.

But it doesnt, since i dont get the output..

tried this, but says "Comparing with boolean with number" @GetDistanceBetweenPoints2D

i do get the 3rd output Thought

  
  
MedKit1 = {} 
MedKit2 = {} 
MedKit3 = {} 
MedKit4 = {} 
  
function MarkerCheckMed() 
outputChatBox("Checking Distance", getRootElement(), 255,255,255,true) 
for i,v in ipairs(getElementsByType("player")) do 
outputChatBox("Checking player "..getPlayerNametagText(v), getRootElement(), 255,255,255,true) 
for s, k in ipairs(MedKit1) do 
outputChatBox("Checking Player Marker ", getRootElement(), 255,255,255,true) 
if getDistanceBetweenPoints3D(k, v) <= 15 then 
outputChatBox("Distance Met!", getRootElement(), 255,255,255,true) 
if not getElementData(v, "Heal") then 
setElementData(v, "Heal", true) 
end 
else 
if getElementData(v, "Heal") then 
removeElementData(v, "Heal") 
end 
end 
end 
end 
end 
MarkerTim = setTimer(function() 
MarkerCheckMed() 
end, 1000, 0) 
  
  
function MedKits(player,cmd) 
if getElementData(player,"MediKits") > 0 then 
meds = getElementData(player, "MediKits") 
local x,y,z = getElementPosition(player) 
setElementData(player, "MediKits", meds - 1) 
outputChatBox("#0077ff[bF3]: MedKit Deployed! Medkits Left: ".. getElementData(player, "MediKits"), player,255,255,255,true) 
if MedKit1[player] then 
MedKit4[player] = createMarker(x,y,z,"corona", 1, 0,255,0,250, getPlayersInTeam(getPlayerTeam(player))) 
table.insert(MedKit2, MedKit3[player]) 
else 
MedKit3[player] = createMarker(x,y,z,"corona", 1, 0,255,0,250, getPlayersInTeam(getPlayerTeam(player))) 
table.insert(MedKit1, MedKit3[player]) 
end 
else 
outputChatBox("#0077ff[bF3]: You dont have any MedKits left!", player,255,255,255,true) 
end 
end 
addCommandHandler("MedKit", MedKits, false,false) 

Link to comment

Just found out a problem: I cant destroy one of the markers, its just there, this is how i destroy upon onPlayerWasted..

The first marker is destroyed, however, the second isnt destroyed.

  
local source = source 
if MedKit3[source] then 
for i,v in ipairs(MedKit1) do 
if v == MedKit3[source] then 
destroyElement(v) 
v = nil 
destroyElement(MedKit3[source]) 
MedKit3[source] = nil 
end 
end 
end 
if MedKit4[source] then 
for i,v in ipairs(MedKit2) do 
if v == MedKit4[source] then 
destroyElement(v) 
v = nil 
destroyElement(MedKit4[source]) 
MedKit4[source] = nil 
end 
end 
end 

Link to comment
  • Moderators
Just found out a problem: I cant destroy one of the markers, its just there, this is how i destroy upon onPlayerWasted..

The first marker is destroyed, however, the second isnt destroyed.

  
local source = source 
if MedKit3[source] then 
for i,v in ipairs(MedKit1) do 
if v == MedKit3[source] then 
destroyElement(v) 
v = nil 
destroyElement(MedKit3[source]) 
MedKit3[source] = nil 
end 
end 
end 
if MedKit4[source] then 
for i,v in ipairs(MedKit2) do 
if v == MedKit4[source] then 
destroyElement(v) 
v = nil 
destroyElement(MedKit4[source]) 
MedKit4[source] = nil 
end 
end 
end 

K, it took me a lot of time to understand your code since I just came in.

First I just wanted to say that medkit3 and 4 are totally useless. As Solidsnake, there is no need to do a table numerical indexed ...

And here is the problem. You are first doing this:

if v == MedKit3[source] then 

so v and MedKit3[source] reference the same marker but you want to delete that marker twice:

destroyElement(v) 
destroyElement(MedKit3[source]) 

So the 2nd call to destroyElement just fail since it's already destroyed so the script just stop there.

Also, there is no need to set v to nil since it's a temp variable used by the loop.

So just remove the lines 6-7 and 16-17 and it will work just fine.

Link to comment
Just found out a problem: I cant destroy one of the markers, its just there, this is how i destroy upon onPlayerWasted..

The first marker is destroyed, however, the second isnt destroyed.

  
local source = source 
if MedKit3[source] then 
for i,v in ipairs(MedKit1) do 
if v == MedKit3[source] then 
destroyElement(v) 
v = nil 
destroyElement(MedKit3[source]) 
MedKit3[source] = nil 
end 
end 
end 
if MedKit4[source] then 
for i,v in ipairs(MedKit2) do 
if v == MedKit4[source] then 
destroyElement(v) 
v = nil 
destroyElement(MedKit4[source]) 
MedKit4[source] = nil 
end 
end 
end 

K, it took me a lot of time to understand your code since I just came in.

First I just wanted to say that medkit3 and 4 are totally useless. As Solidsnake, there is no need to do a table numerical indexed ...

And here is the problem. You are first doing this:

if v == MedKit3[source] then 

so v and MedKit3[source] reference the same marker but you want to delete that marker twice:

destroyElement(v) 
destroyElement(MedKit3[source]) 

So the 2nd call to destroyElement just fail since it's already destroyed so the script just stop there.

Also, there is no need to set v to nil since it's a temp variable used by the loop.

So just remove the lines 6-7 and 16-17 and it will work just fine.

the fact is, that it does not loop the table if it is not numerically indexed (It only worked if i did so)

oh and edit: note that im not deleting double, im destroying its copy, i insert MedKit3[source] into numerical table, so i destroy the numerical copy and the MedKit3[source] one to dont leave anything

Link to comment
  • Moderators
the fact is, that it does not loop the table if it is not numerically indexed (It only worked if i did so)

Of course you can, Solidsnake told you to use pairs instead of ipairs. Read this:

http://www.luafaq.org/#T1.10

oh and edit: note that im not deleting double, im destroying its copy, i insert MedKit3[source] into numerical table, so i destroy the numerical copy and the MedKit3[source] one to dont leave anything

No that's not a copy of the marker at all, just a copy of it's reference. So you are destroying the same marker twice because the two references are pointing to the same marker ...

And seriously, if more experienced developpers are telling you what's your script doing, just try to understand by reading carefully. Don't say they are wrong without testing it. You didn't even try ! I swear there is an error in debugscript 3 you didn't gave us that occurs at line 8.

Regards,

Citizen

Link to comment
the fact is, that it does not loop the table if it is not numerically indexed (It only worked if i did so)

Of course you can, Solidsnake told you to use pairs instead of ipairs. Read this:

http://www.luafaq.org/#T1.10

oh and edit: note that im not deleting double, im destroying its copy, i insert MedKit3[source] into numerical table, so i destroy the numerical copy and the MedKit3[source] one to dont leave anything

No that's not a copy of the marker at all, just a copy of it's reference. So you are destroying the same marker twice because the two references are pointing to the same marker ...

And seriously, if more experienced developpers are telling you what's your script doing, just try to understand by reading carefully. Don't say they are wrong without testing it. You didn't even try ! I swear there is an error in debugscript 3 you didn't gave us that occurs at line 8.

Regards,

Citizen

Well i missed that about just using pairs instead of ipairs, i quickly readed over it and didnt noticed the change. And, i did not got that error on that line. The only error i got was when the script tried to compare the marker with me when the marker did no longer exist.

Anyways, i'm starting up my server to check if it works correctly after the fixes i've done according to pairs.

EDIT:

Just found out: fails to delete them

local source = source 
if MedKit1[source] then 
destroyElement(MedKit[source]) 
end 
if MedKit2[source] then 
destroyElement(MedKit2[source]) 
end 

Error: attempt to index global 'MedKit' (a nil value) - Line 3

Marker creation: (works perfectly)

function MedKits(player,cmd) 
if getElementData(player,"MediKits") > 0 then 
meds = getElementData(player, "MediKits") 
local x,y,z = getElementPosition(player) 
setElementData(player, "MediKits", meds - 1) 
outputChatBox("#0077ff[bF3]: MedKit Deployed! Medkits Left: ".. getElementData(player, "MediKits"), player,255,255,255,true) 
if MedKit1[player] then 
MedKit2[player] = createMarker(x,y,z,"corona", 1, 0,255,0,250, getPlayersInTeam(getPlayerTeam(player))) 
else 
MedKit1[player] = createMarker(x,y,z,"corona", 1, 0,255,0,250, getPlayersInTeam(getPlayerTeam(player))) 
end 
else 
outputChatBox("#0077ff[bF3]: You dont have any MedKits left!", player,255,255,255,true) 
end 
end 
addCommandHandler("MedKit", MedKits, false,false) 

Link to comment
  • Moderators

Your table is called MedKit1 and not MedKit. Also, why did you remove the line that was setting the table cell to nil ? It's nedded with your script:

local source = source 
if MedKit1[source] then 
    destroyElement(MedKit1[source]) 
    MedKit1[source] = nil 
end 
if MedKit2[source] then 
    destroyElement(MedKit2[source]) 
    MedKit2[source] = nil 
end 

Link to comment
Your table is called MedKit1 and not MedKit. Also, why did you remove the line that was setting the table cell to nil ? It's nedded with your script:
local source = source 
if MedKit1[source] then 
    destroyElement(MedKit1[source]) 
    MedKit1[source] = nil 
end 
if MedKit2[source] then 
    destroyElement(MedKit2[source]) 
    MedKit2[source] = nil 
end 

Yeah, after i just posted i realized and fixed it, the nil part you told me before to remove it, but as while i was fixing it i readed https://wiki.multitheftauto.com/wiki/DestroyElement and noticed the nil thingy.

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