✓ Accepted answer
In addition to what Spongocardo said:
*while Random == Random2 do will loop forever when #Leaves is 1, since math.random(1, 1) will always return 1. .
*math.random can be called without a second argument (i.e. using math.random(10) will return a random integer from 1 to 10)
*The while true do loop in the bottom will run even if the tree doesn't need to wilt. While this might not be a problem when there's only one tree, it will when you have more.
*In addition, the loop will never stop even if there are no leaves left in the tree.
*This program overall is poorly designed (what if you want to change how many leaves are removed per tick? What if you want to change how the leaves disappear?)
Try this:
local Trunk = script.Parent:WaitForChild("Trunk")
local Leaves = script.Parent:WaitForChild("Leaves")
local WILT_DELAY = 0.5
local WILT_AMOUNT = 2
function Wilt()
Leaves_Parts = Leaves:GetChildren()
local function ShedLeaf(leaf)
leaf:Destroy()
end
local function WiltStep(number)
for i = 1, math.min(number, #Leaves_Parts) do
local rand = math.random(#Leaves_Parts)
ShedLeaf(Leaves_Parts[rand])
table.remove(Leaves_Parts, rand)
end
end
while #Leaves_Parts > 0 do
wait(WILT_DELAY)
WiltStep(WILT_AMOUNT)
end
end
Trunk.ChildRemoved:connect(function (_)
local Trunk_Parts = Trunk:GetChildren()
if #Trunk_Parts == 0 then
Wilt()
end
end)
2 comments
Nothing works with it, added output in a prntscr.
is return suposed to be break?