Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

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

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

3 comments

0

Try to use tree.Position = Vector3.new(-52.751, 50.888, -54.404) Instead

0

still wont work.

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?

3 answers

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!

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)

2 comments

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?

0

well yeah thats works if you do .Changed so that is prob better

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)

This discussion is preserved as it was. New answers, comments, and votes are closed.