neff111
|
  |
| Joined: 15 Nov 2010 |
| Total Posts: 65 |
|
|
| 24 Jun 2014 09:06 PM |
I need help detecting if a certain thing is already in a table. This is what I have:
PlayerShip = {}
Ship=Player:GetMouse().Target.Parent for iiii = 0,#PlayerShip+1 do print(PlayerShip[iiii],Ship.Name) -- This is just so I can probly find the problem if Ship~=PlayerShip[iiii] then table.insert(PlayerShip,Ship) end end
I have no good idea what the problem is. Does anyone have advise? |
|
|
| Report Abuse |
|
|
Goulstem
|
  |
| Joined: 04 Jul 2012 |
| Total Posts: 7177 |
|
|
| 24 Jun 2014 09:09 PM |
PlayerShip = {} Ship = Player:GetMouse().Target.Parent
for i,v in pairs(PlayerShip) do if Ship ~= v then table.insert(PlayerShip,Ship) end end ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Uhm.. you're entering the value into the table IF it's close enough to one that's already in the table. What's the point of this? Cleaned it up for you. |
|
|
| Report Abuse |
|
|
neff111
|
  |
| Joined: 15 Nov 2010 |
| Total Posts: 65 |
|
|
| 24 Jun 2014 09:12 PM |
Rocket Science :3 Jk
Im trying to create a style of game where you controll a fleet of space ships, but I was having the problem of accedently selecting a ship more than once and breaking the script :I |
|
|
| Report Abuse |
|
|
neff111
|
  |
| Joined: 15 Nov 2010 |
| Total Posts: 65 |
|
| |
|
|
| 24 Jun 2014 09:16 PM |
You could make the ship a key:
local t = {} local n = 0
function deselect() t = {} n = 0 end
function select(object) n = n + 1 t[object] = n end
Now you select an object twice and it wont matter, and you can use next to iterate through the table. |
|
|
| Report Abuse |
|
|