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

Variable doesn't seem to be working?

I have made a chop-able tree script and it works, sort of.

local a = script.Parent
local b = script.Parent.Log
local c = script.Parent.Log2
local d = script.Parent.Log3
local e = script.Parent.Log4
local f = script.Parent.Leaves
local g = script.Parent.Leaves2
local h = script.Parent.Leaves3
local i = script.Parent.Leaves4
local hits = 0
local hitsN = 16
b.Touched:connect(function(hit)
	if hit.Parent.Name == "Basic Axe" then
		hits = hits + 1
		
		if hits == hitsN then
			hits = 0
			b.Anchored = false
			c.Anchored = false
			d.Anchored = false
			e.Anchored = false
			f.Anchored = false
			g.Anchored = false
			h.Anchored = false
			i.Anchored = false
			f.CanCollide = false
			g.CanCollide = false
			h.CanCollide = false
			i.CanCollide = false
		end
	end
end)

the "local hitsneeded" is setting the hits needed to 16 so that once the tree has been hit 16 tims, all leaves will fall and the logs will become unanchored, but for some reason, the tree falls the first time that i hit the tree. I hit the tree with the "Basic Axe" tool i have in my inventory and it crumples on the first hit instead of sixteenth, can i get some help? thanks :)

3 comments

0

(I know it isn't LUA but the main script its self (the actual making the tree fall) works in roblox.

0

Thanks for the reply! Do you know how I could set up the script with a ClickDetector maybe?

2 answers

✓ Accepted answer

Are you sure that all the 16 hits don't happen in quick succession? It's possible that when you hit the tree it triggers the command 16 times because there is no wait intervals between when it can be hit, so it is allowed to land the second hit milliseconds after the first. Try this http://wiki.roblox.com/index.php?title=Debounce

A nice way to fix this would be with a debounce (http://wiki.roblox.com/index.php?title=Debounce), since it seems to me that it's repeatedly adding one to your variable, as you are using "Touched:connect". Another way is to use a click detector. Have fun coding!

1 comment

0

Thanks for the response! I don't know how to make it into a ClickDetector because I wouldn't know how to set that up with all the variables, but I'll try a debounce for now. Thanks!

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