So I’ve been working on an inventory system, and There is in total 36 slots. For some reason I cannot understand, This script under, returns the 36th, even though every single slot before that is also “Air”
if AddedToSlot == false then
local allinv = invslot:GetChildren()
local findnewslot = false
print("new slot time")
for i=1, #allinv do
if findnewslot == false then
if allinv[i].Value == "Air" then
print("Found new slot")
findnewslot = true
allinv[i].Value = itemname
allinv[i].Amount.Value = 1
end
end
end
I have no idea what makes this return the 36th slot, because every other slot have their value also as “Air”.
if AddedToSlot == false then
local allinv = invslot:GetChildren()
local findnewslot = false
for i in ipairs(allinv) do
if findnewslot == false then
if allinv[i].Value == "Air" then
print("Found new slot")
findnewslot = true
allinv[i].Value = itemname
allinv[i].Amount.Value = 1
end
end
end
If you could share a bit more code I might be able to help understand why you’re getting the current problem, but one thing you can do is add in a break statement inside the for loop once it finds the air value so it stops trying to process.
You could also add a few more print lines, I’d put one on each line, and probably have it output some relevant information to see what’s going on, like the first line of your for loop you can specify to print findnewslot
function inventory.GiveItem(chr,itemname)
local AddedToSlot = false
local plr = game.Players:GetPlayerFromCharacter(chr)
if plr ~= nil then
print(plr.Name.." will recieve the item: "..itemname.."")
local invslot=plr:FindFirstChild("InventorySlots")
if invslot ~= nil then
print("invslots found")
local allinv = invslot:GetChildren()
for i=1, #allinv do
if AddedToSlot == false then
if allinv[i].Value == itemname then
if allinv[i].Amount.Value <= 64 then
allinv[i].Amount.Value = allinv[i].Amount.Value + 1
print("added to a different slot")
AddedToSlot = true
else
end
end
end
end
end
if AddedToSlot == false then
local allinv = invslot:GetChildren()
local findnewslot = false
print("new slot time")
for i=1, #allinv do
if findnewslot == false then
if allinv[i].Value == "Air" then
print("Found new slot")
findnewslot = true
allinv[i].Value = itemname
allinv[i].Amount.Value = 1
end
end
end
end
end
end
It either checks for if there’s a stack to join or to start a completely new stack, thats literally it, the stack part of it works fine, but starting a new stack always starts it at 36.
I wonder if this is because GetChildren isn’t guaranteed to get all children in order. If all the children of invslot can be renamed 1, 2, 3, etc, you can do this.
local inv1 = invslot["1"]
for i = 1, 36 do
...
if invslot[tostring(I)].Value == "Air" then