So what the code is suppose to do, when there are no more Parts in 'Trunk' then it randomly select a Part from Leaves and remove it, here's the code:
01 | function DLeaves() |
02 | local Leaves = script.Parent:WaitForChild( "Leaves" ):GetChildren() |
03 | local Trunk = script.Parent:WaitForChild( "Trunk" ):GetChildren() |
04 | if #Leaves = = 0 then |
05 | return ; |
06 | end |
07 |
08 | if #Trunk = = 0 then |
09 | Random = math.random( 1 , #Leaves) |
10 | Random 2 = Random |
11 |
12 | while Random = = Random 2 do |
13 | Random 2 = math.random( 1 , #Leaves) |
14 | wait() |
15 | end |
Output: http://prntscr.com/9qk5gy
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:
01 | -- Load some variables into the script |
02 | local Trunk = script.Parent:WaitForChild( "Trunk" ) |
03 | local Leaves = script.Parent:WaitForChild( "Leaves" ) |
04 | -- Delay between wilting steps |
05 | local WILT_DELAY = 0.5 |
06 | -- Amount of leaves to wilt per step |
07 | local WILT_AMOUNT = 2 |
08 |
09 | -- Once called, the leaves on the tree will start to wilt |
10 | function Wilt() |
11 | Leaves_Parts = Leaves:GetChildren() |
12 | -- Called whenever a leaf is shed. |
13 | local function ShedLeaf(leaf) |
14 | leaf:Destroy() |
15 | end |
The problem is that if there are parts in the trunk, the Random and Random2 variables will never be assigned, therefore the script would be trying to destroy a nil value. This is why the output says that you're attempting to index 'Chosen', which would be a nil value.
To fix this, you should have the Destroy method(s) inside of the if statement so that the leaves are only destroyed when the number of parts in 'Trunk' equals 0, which will be when the random variables are assigned.
01 | function DLeaves() |
02 | local Leaves = script.Parent:WaitForChild( "Leaves" ):GetChildren() |
03 | local Trunk = script.Parent:WaitForChild( "Trunk" ):GetChildren() |
04 | if #Leaves = = 0 then |
05 | return ; |
06 | end |
07 |
08 | if #Trunk = = 0 then |
09 | local Random = math.random( 1 , #Leaves) --It's quicker to access local variables than global ones. |
10 | local Random 2 = Random --Same as above. |
11 |
12 | while Random = = Random 2 do |
13 | Random 2 = math.random( 1 , #Leaves) --No need for local before Random2 here since we've already assigned Random2 earlier. |
14 | wait() |
15 | end |
I hope my answer helped you. If it did, be sure to accept it.