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

Help me with tree chopping script?

Asked by
Yeevivor4 155
9 years ago
hits = script.Parent.hit --This keeps track of how much "hp" the tree has left
local newtree = script.Parent:clone() --for inserting a new tree later

while true do --infinite loop
if hits.Value < 1 then --if the tree has less than 1 "hp" then
    script.Parent:BreakJoints() --make tree able to fall over. 
    wait(3) --wait a while
    script.Parent:remove() --remove the tree
    wait(10) --wait a while
    newtree.Parent = game.Workspace --insert a new tree
    newtree:MakeJoints() --make tree unable to fall over
end
wait(1) --wait a while before checking trees hp again
end

So in this model, I have a model that has a tree, and this script inside of the model. When I first use an axe to hit the tree, it breaks and give me money. After that, it regenerates, but when it regenerates it gives me this line. Workspace.Tree.FellCheck:6: attempt to index field 'Parent' (a nil value)

How do I make it so that there is a parent?

0
Have you tried to play it solo and look at the hierarchy in explorer as this regenerates? I believe you may just be cloning a part, not the whole model? alphawolvess 1784 — 9y

2 answers

Log in to vote
0
Answered by
Discern 1007 Moderation Voter
9 years ago

When your tree regenerates, Line 6 is trying to break the script's parent's joints. What's happening is that your loop is running so fast that it runs the loop before the tree is even there. You should wait before the parent is there, and you can clone it after the script recognizes its parent.

hits = script.Parent.hit --This keeps track of how much "hp" the tree has left
local newtree = script.Parent:clone() --for inserting a new tree later

while true do --infinite loop
if hits.Value < 1 then --if the tree has less than 1 "hp" then
    repeat wait() until script.Parent --Waits until the script has a parent.
    script.Parent:BreakJoints() --make tree able to fall over. 
    wait(3) --wait a while
    script.Parent:remove() --remove the tree
    wait(10) --wait a while
    newtree.Parent = game.Workspace --insert a new tree
    newtree:MakeJoints() --make tree unable to fall over
end
wait(1) --wait a while before checking trees hp again
end

If I helped you out, make sure to hit the Accept Answer button below my character! :D

Ad
Log in to vote
0
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
9 years ago

The error is telling you that script.Parent is nil.

Look at the script what might cause this:

script.Parent:remove()

:remove() will recursively break all objects off from their parents, thus script.Parent is nil after that line.



Really, this script, as it is currently structured, shouldn't use a while true do loop.

What you are doing is waiting until the tree dies; then you get rid of the tree, and put in a fresh on (which has its own new script, anyway).

Thus this might be more straightforward

local model = script.Parent -- You say this a lot, so make it a variable!
local hits = model.hit
local newTree = model:Clone()

repeat
    -- Do this (wait)
    wait(1)
    -- until this tree is dead
until hits.Value < 1

model:BreakJoints()
wait(3)
model:Remove()
wait(10)
newTree.Parent = workspace
newTree:MakeJoints()

In general, it would be better to not have the script be part of the model, though this would require a bit of re-architecturing.

Answer this question