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

for some reason if the health is 0 it wont move? Help Please?

Asked by 3 years ago
Edited 3 years ago
local tree = script.Parent
local health = 10
if health == 0 then
    tree.Position = Vector3.new(-52.751, 50.888, -54.404)
end
tree.Touched:connect(function(hit)
    if hit.Name == "Blade" then
        health = health - 1
        print("Player Has Cut Tree")
  end
end)

there are no errors

0
Try to use tree.Position = Vector3.new(-52.751, 50.888, -54.404) Instead RazzyPlayz 497 — 3y
0
still wont work. Configuator 158 — 3y
0
Have u tried to used a int or numbervalue as its health and change it when its touched, then link it up with a changed event or something? DeUltimate23 142 — 3y

3 answers

Log in to vote
1
Answered by 3 years ago

Your code is only checking the health once. The script reads what you've written up to tree.Touched:connect... once and then just listens for the tree being touched, every time the tree is touched the code in that funciton runs. So just have the script check the health everytime the tree is touched :)

local tree = script.Parent
local health = 10

tree.Touched:connect(function(hit) --when the tree is touched...

    if hit.Name == "Blade" then --if hit by a blade then...
            health = health - 1 --take 1HP
            print("Player Has Cut Tree")
        if health == 0 then --check if the HP has reached 0
            tree.Position = CFrame.new(tree.Position) + Vector3.new(-52.751, 50.888, -54.404)) 
        else print(health) --prints the curent HP of the tree
        end
    end

end)

Hopefully that is clear and works alright... :) Best of luck, let me know if this helped!

Ad
Log in to vote
0
Answered by 3 years ago

OK what your doing wrong is it will only check once if the health is 0. At the beginning it's 10. What you have to do is this:

local tree = script.Parent
local health = 10
while true do
    if health == 0 then
            tree.Position = Vector3.new(-52.751, 50.888, -54.404)
    end
    wait()
end
tree.Touched:connect(function(hit)
    if hit.Name == "Blade" then
        health = health - 1
        print("Player Has Cut Tree")
  end
end)
0
No disputing the answer but out of interest does running lots of "while true do" loops in a game slow performace? You'd think just cheking for a change would be more effective? AlexTheCreator 461 — 3y
0
well yeah thats works if you do .Changed so that is prob better techingenius -82 — 3y
Log in to vote
0
Answered by 3 years ago

You've done 3 things wrong 1 a debouncer 2 detecting if the health is still 0 when touched 3rd you should have a Detector for when the health reaches 0

local tree = script.Parent
local health = 10

local Debounce = true

game:GetService("RunService").Heartbeat:Connect(function()
if health == 0 then
    tree.Position = Vector3.new(-52.751, 50.888, -54.404)
end
end)

tree.Touched:connect(function(hit)
if Debounce == false then return end
    if hit.Name == "Blade and health >0" then
Debounce = false
        health = health - 1
wait(0) ---Change time if want the wait to chop it again to be longer
Debounce = true
 end
end)

Answer this question