#What's the correct way to make this work?

1 messages · Page 1 of 1 (latest)

zenith geyser
#

I want to make a part in the script where at the start, there's a lot of children in different items inside one folder named items, and i want to reduce them to maxStock, in this case, 8, this is my current code

#
local gs1 = workspace:WaitForChild("Map"):WaitForChild("GasStation1")
local store = gs1:WaitForChild("Store")
local interior = store:WaitForChild("Interier")
local register = interior:WaitForChild("Register"):WaitForChild("CashRegister")
local drawer = register:WaitForChild("CashDrawer")
local drawerprompt = drawer:WaitForChild("ProximityPrompt")

local itemsFolder = interior:WaitForChild("Items")
local maxStock = 8

for _, itemModel in ipairs(itemsFolder:GetChildren()) do
    local children = itemModel:GetChildren()
    for i = maxStock + 1, #children do
        children[i]:Destroy()
    end
end```
sharp lichen
#

how is code formatted so ahh on phones

#

nvm

delicate mica
sharp lichen
#

Jaimz help this guy i cant write code on my phone🥀🥀

delicate mica
#

local gs1 = workspace:WaitForChild("Map"):WaitForChild("GasStation1")
local store = gs1:WaitForChild("Store")
local interior = store:WaitForChild("Interier")
local register = interior:WaitForChild("Register"):WaitForChild("CashRegister")
local drawer = register:WaitForChild("CashDrawer")
local drawerPrompt = drawer:WaitForChild("ProximityPrompt")

local itemsFolder = interior:WaitForChild("Items")
local maxStock = 8

for _, itemModel in ipairs(itemsFolder:GetChildren()) do
local parts = {}
for _, child in ipairs(itemModel:GetChildren()) do
if child:IsA("BasePart") then
table.insert(parts, child)
end
end

if #parts > maxStock then
    table.sort(parts, function(a, b)
        return a.Name < b.Name
    end)

    for i = maxStock + 1, #parts do
        parts[i]:Destroy()
    end
end

end

zenith geyser
#

thanks

hardy dove
#

first of all

#

do not use that many WaitForChild

#

thats useless

#

just use points

#

cause most of the things that spawns in the game they spawn before any script

#

so no need to use that many wait for child

#

WaitForChild() is misused all the time, and I'm guilty of it too. Hopefully, this video will shed some light on when and when NOT to use WaitForChild()!

Have you ever wanted to learn Roblox Studio and Roblox Scripting? Check out my course: https://bit.ly/LuaScriptingCourse
Use code DEVELOPER for 33% off!

My linktree:
https://linktr.ee/crusherf...

▶ Play video
#

watch this

zenith geyser
alpine quarry
hardy dove
#

yea

ocean quartz
#

for i, obj in itemModel:GetChildren() do
if i <= maxStock then
obj:Destroy()
end
end

final narwhalBOT
#

studio** You are now Level 1! **studio

ocean quartz
#

But I assume this is for a “drawer” or “cash register” type thing