I am need to figure out how I could remove a [certain] number of items from a table and ONLY that amount?
I have this Shiny system for my game revolving around pets and players need 10 pets to make that pet shiny. What’s the best way to remove 10 of that exact same pet from their table whilst retaining the others if they have more of that same pet? The indexes are obviously going to be random (saying this by the index of whatever pet is the same as the one they’re trying to make shiny)
So really, what’s the best way to remove an exact amount of specified items from a table?
Uh? What? Every pet has their own table of pet data, I can’t have an ‘amount’ value. Every pet has their own xp, level, etc, so an ‘amount’ value with 1 table cannot be made. It’s not inefficient.
You could create a function and call it local function removePetFromTable(table, petName, amountToRemove)
Then you could add the functionality like so:
local function removePetFromTable(petTable, petToRemove, amountToRemove)
for i = 1, amountToRemove, 1 do
if petTable[i].Name == petToRemove then
table.remove(petTable, i)
end
end
end
Then you could call it like this.
local pets = {...}
removePetFromTable(pets, "Green Cat", 10)
It wouldn’t change the table though, maybe return the new edited table and set it as pets, i’m pretty sure it wouldn’t replicate from removePetsFromtable function to the actual script.
Yeah it’s because he/she is only checking 10 of the first pets in the table, when they should be checking all of them and then breaking the loop after 10 pets removed. I’m sure you can add that yourself.
local function removePetFromTable(petTable, petToRemove, amountToRemove)
local amountRemoved = 0
while (amountRemoved < amountToRemove) do
local petFound = false
for i = 1, #petTable, 1 do
if petTable[i] and petTable[i].Name == petToRemove then
table.remove(petTable, i)
amountRemoved = amountRemoved + 1
petFound = true
break
end
end
if not petFound then
break
end
end
end
local function removePetFromTable(petTable, petToRemove, amountToRemove)
local amountRemoved = 0
local petNotExistant = false
while (amountRemoved < amountToRemove) and not petNotExistant do
for i = 1, #petTable, 1 do
if petTable[i] and petTable[i].Name == petToRemove then
print(petTable[i].Name)
table.remove(petTable, i)
amountRemoved = amountRemoved + 1
break
end
end
petNotExistant = true
end
end
Hey I just wanted to give this to you. Because I fixed all the edge cases.
local function removePetFromTable(petTable, petToRemove, amountToRemove)
local amountRemoved = 0
while (amountRemoved < amountToRemove) do
local petFound = false
for i = 1, #petTable, 1 do
if petTable[i] and petTable[i].Name == petToRemove then
table.remove(petTable, i)
amountRemoved = amountRemoved + 1
petFound = true
break
end
end
if not petFound then
break
end
end
end