I’m making an auto shirt giver, which is basically has two tables like this:
local ClothesList={}
ClothesList.Shirts={}
ClothesList.Pants={}
The script first loops through the two tables and removes entries that are not numbers.
Next, I have a “Dummies” folder in workspace that contains dummies that will display the shirts and pants in the tables.
The script loops through this folder, and checks if the dummy has a shirt object, if not, it creates one, and sets the id to a random one from the table.
However, the script sets the id, but the shirt doesn’t display. If I try to set the same id manually, the end of the id changes, and the shirt displays.
The for loop for applying shirt:
for n,dy in pairs(Dummies:GetChildren()) do
if dy:IsA("Model") and dy:FindFirstChild("Humanoid") then
local isShirtFound=dy:FindFirstChildWhichIsA("Shirt")
if isShirtFound==nil or isShirtFound==false then
local nShirt=Instance.new("Shirt",dy)
nShirt.ShirtTemplate="rbxassetid://"..tostring(ClothesList.Shirts[n])
ClothesList.Shirts[n]=nil
end
end
end
How could I fix this problem?
EDIT: It removes the used id from the shirts table!
This immediately set off an alarm in my head, how exactly are you removing these entries? are you looping through the table and then in the same loop using table.remove?
this makes me think that this is the case. Theres a huge problem with doing this, when you remove a value from the table, the position of all the indices shifts, however your for loop does not take this into account! This could be the reason its removing ids that you dont want and having side effects.
The solution? iterate backwards
for i=#ClothesListShirts,1,-1 do
--your logic here
--if something then table.remove(i)
end
for n,ab in pairs(ClothesList.Shirts) do
if typeof(ab)~="number" then
warn("Entry "..n.." is not a number in Shirts list!")
ClothesList.Shirts[ab]=nil
end
end
I tested it and it worked properly, however the entry that was not a number was the 3. entry and the first and second entries were numbers.
in my testing this has also been a bad practice with terrible results, these are my results
local t={'5312',12345,'notnum','shirt',12412,985743,'uhoh'}
local function method1(tbl)
for i,v in pairs(tbl) do
if typeof(v)~= 'number' then
t[i]=nil
end
end
return tbl
end
local function method2(tbl)
for i=#tbl,1,-1 do
if typeof(tbl[i])~= 'number' then
table.remove(tbl,i)
end
end
return tbl
end
method 1 printed {nil,12345}
it just ate up a bunch of numbers, and now theres nil in my table!
while method 2 printed {12345,12412,985743}
worked perfectly!