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

How come my tree can not run it's script again?

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

Hello, thank you for reading! What I'm trying to do is that when a tool named "Axe" hits and takes all the tree's HP, then the tree will die and give you money. My problem is that the tree dies, and after it regenerates, it's script doesn't work. You can't kill it again. This is a new edited script I tried to do.

hits = script.Parent.hit --This keeps track of how much "hp" the tree has left
local newtree = game.Lighting.newtree2 --for inserting a new tree later

while true do --infinite loop
    debounce = true
if hits.Value < 1 then
    if script.Parent:isA("Model") 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(5) --wait a while
    newtree.Parent = game.Workspace --insert a new tree
    newtree:MakeJoints() --make tree unable to fall over
    debounce = false
end
wait(1) --wait a while before checking trees hp again
end
end

What I added was the if script.Parent:isA("Model") then part, but when I Play in the game, the server does not start. I think it is because of the while true do part, which causes the game to repeat the scripts so much the server doesn't lag.

Sorry for the long explanation, and if anyone can help me, I would be so grateful!

0
You should use the Changed event and not a while loop. NotsoPenguin 705 — 9y
0
How would I do that? I don't know what the Changed Event is. Yeevivor4 155 — 9y

2 answers

Log in to vote
0
Answered by 9 years ago

You mispelled it, capitalize the i in isA() ---->

if script.Parent:IsA("Model") then [INSERT CODE HERE] end

Ad
Log in to vote
0
Answered by 9 years ago

You can use the Changed event like this:

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

hits.Changed:connect(function()
    if hits.Value < 1  then 
        script.Parent:BreakJoints()
        wait(3)
        script.Parent:remove() 
        wait(10) 
        newtree.Parent = game.Workspace 
        newtree:MakeJoints()
    end
end)

Answer this question