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

Variable doesn't seem to be working?

Asked by 6 years ago
Edited 6 years ago

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 :)

0
(I know it isn't LUA but the main script its self (the actual making the tree fall) works in roblox. XHorizonSwiftX 4 — 6y
0
Thanks for the reply! Do you know how I could set up the script with a ClickDetector maybe? XHorizonSwiftX 4 — 6y

2 answers

Log in to vote
0
Answered by 6 years ago

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

Ad
Log in to vote
0
Answered by 6 years ago

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!

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! XHorizonSwiftX 4 — 6y

Answer this question