Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

Getting result from a table?

Asked by
iFlusters 355 Moderation Voter
8 years ago

I have a table, and I am wondering how I can get the result form it.

items = {
    {chance = 5, item = ChestItems:WaitForChild("Pickaxe")}
}

I know I can simply do items.chance, but how do I get the chance of a specific chosen item, from this:

Item = chooseItem():Clone()
    Item.Parent = Player.Backpack

chooseItem() is a function which randomly chooses an item from items using the 'chance' as a percentage on getting it.

So I was wondering how I could use the 'chance' from the clone'd item. So I could do this:

if chance <= 5 then

Any help would be appreciated.

1 answer

Log in to vote
0
Answered by 8 years ago

With difficulty
Unless you change how it works

Your current setup means that in order to find the chance for an item, you do in fact have to iterate through the entire table until you find the item. For your setup, this should suffice...

function getChance(i)
 local inn = i.Name
 for n=1,#items do
  local v = items[n];
  if inn == v.item.Name then -- Probably not an unfair assumption. We can't check for object equality because they're not equal.
   return v.chance;
  end
 end
 return 0;
end

This isn't the best way, but without further changing of how your system is set up (And I prefer not to mess with other people's understandings of how things relate) this is probably the best you've got.

Ad

Answer this question