Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
2

Random Event then Destory() not working?

Asked by
iFlusters 355 Moderation Voter
9 years ago

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:

01function 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        Random2 = Random
11 
12        while Random == Random2 do
13            Random2 = math.random(1, #Leaves)
14            wait()
15        end
View all 26 lines...

Output: http://prntscr.com/9qk5gy

0
Nothing works with it, added output in a prntscr. iFlusters 355 — 9y
0
is return suposed to be break? lukeb50 631 — 9y

2 answers

Log in to vote
2
Answered by
XAXA 1569 Moderation Voter
9 years ago

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
02local Trunk = script.Parent:WaitForChild("Trunk")
03local Leaves = script.Parent:WaitForChild("Leaves")
04-- Delay between wilting steps
05local WILT_DELAY = 0.5
06-- Amount of leaves to wilt per step
07local WILT_AMOUNT = 2
08 
09-- Once called, the leaves on the tree will start to wilt
10function Wilt()
11    Leaves_Parts = Leaves:GetChildren()
12    -- Called whenever a leaf is shed.
13    local function ShedLeaf(leaf)
14        leaf:Destroy()
15    end
View all 44 lines...
0
I appreciate what you are saying, and your help too. But I am not very good at coding so please don't criticise me, after all this is a help site. iFlusters 355 — 9y
0
Very good answer, upvoted. Spongocardo 1991 — 9y
Ad
Log in to vote
2
Answered by 9 years ago

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.


01function 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 Random2 = Random --Same as above.
11 
12        while Random == Random2 do
13            Random2 = math.random(1, #Leaves) --No need for local before Random2 here since we've already assigned Random2 earlier.
14            wait()
15        end
View all 24 lines...

I hope my answer helped you. If it did, be sure to accept it.

Answer this question