Jeremy15 Posted January 4, 2017 Posted January 4, 2017 Hello community, I am having some trouble with destroying a little group of objects. I have managed to create a few billboards in 1 group but when i try to trigger the command to remove all of them. it just removes 1 object instead of them all bill = { [1] = {x = 4308.484375, y = -1278.0102539063, z = 354.45336914063, rotX = 0, rotY = 0, rotZ = 254.53720092773, Scale = 0.80000001}; [2] = {x = 4661.1708984375, y = -1298.7310791016, z = 157.84288024902, rotX = 0, rotY = 0, rotZ = 254.51000976563, Scale = 1.75}; [3] = {x = 4402.3256835938, y = 604.70196533203, z = 53.522163391113, rotX = 0, rotY = 0, rotZ = 270, Scale = 1.5}; [4] = {x = 6365.841796875, y = 2818.9069824219, z = 44.355319976807, rotX = 0, rotY = 0, rotZ = 0, Scale = 2}; [5] = {x = 1899.8275146484, y = 3626.1596679688, z = 62.15608215332, rotX = 0, rotY = 0, rotZ = 295.78491210938, Scale = 2.5}; [6] = {x = 3041.55078125, y = 3309.5639648438, z = 63.786605834961, rotX = 348, rotY = 0, rotZ = 20, Scale = 1}; [7] = {x = 7046.5922851563, y = 848.14624023438, z = 101.83802032471, rotX = 0, rotY = 0, rotZ = 215.10675048828, Scale = 2}; [8] = {x = 13.697020530701, y = -3669.009765625, z = 104.45007324219, rotX = 0, rotY = 0, rotZ = 180, Scale = 1.2}; [9] = {x = -367.28112792969, y = -4859.8872070313, z = 92.081260681152, rotX = 0, rotY = 0, rotZ = 90, Scale = 2.5}; } bills = {} stateMode = 0 function toggleBillboard(cmd) if isElement(getLocalPlayer()) then if stateMode == 0 then stateMode = stateMode + 1 for i, bill in ipairs(bill) do object = createObject(7911, bill.x, bill.y, bill.z, bill.rotX, bill.rotY, bill.rotZ) setElementCollisionsEnabled(object, false) setElementFrozen(object, true) setObjectScale(object, bill.Scale) table.insert(bills, i) end else stateMode = 0 for i, b in ipairs(bills) do destroyElement(b) end end end end addCommandHandler("toggleBills", toggleBillboard) i don't know what i am doing wrong and i really need to get this done.
Tails Posted January 4, 2017 Posted January 4, 2017 First loop is wrong. You're not actually storing the object in the table. It should be like this: for i, bill in ipairs(bill) do bills[i] = createObject(7911, bill.x, bill.y, bill.z, bill.rotX, bill.rotY, bill.rotZ) setElementCollisionsEnabled(bills[i], false) setElementFrozen(bills[i], true) setObjectScale(bills[i], bill.Scale) end
Jeremy15 Posted January 5, 2017 Author Posted January 5, 2017 It's solved now. thanks you. i didn't knew you could use a table as element to create objects for it. i though i need to use table.insert to get it done. anyway i appreciate your help 1
pa3ck Posted January 5, 2017 Posted January 5, 2017 The problem wasn't the way you inserted it, but the value you inserted. You had table.insert(bills, i) which should have been table.insert(bills, object) 1
Tails Posted January 5, 2017 Posted January 5, 2017 (edited) Yes, what pa3ck said as well, I should've mentioned that, sorry. This is just the way I prefer to do it usually and it's also faster especially within a render. Usually I'd write something like this: for i=1,#bill do local bill = bill[i] bills[i] = createObject(7911, bill.x, bill.y, bill.z, bill.rotX, bill.rotY, bill.rotZ) setElementCollisionsEnabled(bills[i], false) setElementFrozen(bills[i], true) setObjectScale(bills[i], bill.Scale) end Edited January 5, 2017 by Tails
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