I’m currently making a game about chopping trees with axes. Everything works except one annoying bug that I can’t fix from several hours. I wanted to ask someone more experienced. The problem is that when timing right the [layer can start chopping several trees at once. Obviously that’s not the point but I think I tried everything to fix it. I tried with chat gpt and grok, they solved it but they changed the whole script which I don’t want because I want to understand everything in the script. That’s the reason why I’m here.
Anyways here a video of the problem:
And here is the main code which is the cause of the problem:
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local hrp = character:WaitForChild("HumanoidRootPart")
local axe = game.ReplicatedStorage.Axe:Clone()
axe.Parent = workspace
local Parts = axe:GetChildren()
local targetCFrame
local Goals = {}
local TweenService = game:GetService("TweenService")
local TweenInfoData = TweenInfo.new(
0.8,
Enum.EasingStyle.Cubic,
Enum.EasingDirection.Out,
0,
false
)
local TweenInfoBarData = TweenInfo.new(
0.2,
Enum.EasingStyle.Sine,
Enum.EasingDirection.Out,
0,
false
)
local TweenInfoChop = TweenInfo.new(
0.2,
Enum.EasingStyle.Cubic,
Enum.EasingDirection.InOut,
0,
false
)
local leaderstats = player:WaitForChild("leaderstats")
local Coins = leaderstats:WaitForChild("Coins")
local Wood = leaderstats:WaitForChild("Wood")
local SpecialWood = leaderstats:WaitForChild("Special Wood")
local Trees = game.Workspace.Trees:GetChildren()
local x = -2
local Following = true
local MovementTween
local Clicked = false
local oldtree
local chopping = false
local choppingTime = 1 -- can be changed
local damage = 5 -- can be changed
for i, tree in pairs(Trees) do
local ClickDetector = tree:FindFirstChild("ClickDetector")
local OriginalHealth = tree:FindFirstChild("Health"):FindFirstChild("Health").Value
local HealthGui = tree:FindFirstChild("Health")
HealthGui.Enabled = false
local function chop(tree)
--print("start")
HealthGui.Enabled = true
--print("chop")
local Health = HealthGui:FindFirstChild("Health")
local Frame = HealthGui:FindFirstChild("Frame")
local Bar = Frame:FindFirstChild("Bar")
local TextLabel = Frame:FindFirstChild("Text")
game.ReplicatedStorage.TakeDamage:FireServer(Health, damage)
--Health.Value -= damage
local BarGoals = { Size = UDim2.new((Health.Value - damage) / OriginalHealth, 0, 1, 0) }
local ChageSizeTween = TweenService:Create(Bar, TweenInfoBarData, BarGoals)
ChageSizeTween:Play()
TextLabel.Text = Health.Value - damage .. "/" .. OriginalHealth
if Health.Value - damage <= 0 then
Bar.Size = UDim2.new(0, 0, 1, 0)
TextLabel.Text = "0/".. OriginalHealth
--SpecialWood.Value += 1
print("yay")
local Event = game.ReplicatedStorage.DestroyedTree
Event:FireServer(tree)
chopping = false
Following = true
end
game.ReplicatedStorage.Chop:FireServer()
--[[Coins.Value += math.random(1,5)
Wood.Value += math.random(1,3)]]
task.wait(1)
--print("end")
end
local function onChopping(tree)
while true do
if chopping == true then
print("tui")
chopping = true
chop(tree)
else
break
end
end
end
ClickDetector.MouseClick:Connect(function()
if Clicked == false then
Clicked = true
print("Clicked")
targetCFrame = tree.main.CFrame * CFrame.new(0, 0, 3.5)
task.wait(choppingTime)
end
end)
local function onClicked(tree)
print(tree)
Clicked = not Clicked
if tree == oldtree then
print("oh no")
--Following = true
else
chopping = false
oldtree = tree
Following = false
for _, part in pairs(Parts) do
--print(part.Name)
if part.Name == "top" then
local targetCFrame = tree.main.CFrame * CFrame.new(-1.35, x, 5.95) * CFrame.Angles(0, 0, math.rad(90))
Goals = { CFrame = targetCFrame }
elseif part.Name == "middle" then
local targetCFrame = tree.main.CFrame * CFrame.new(0, x, 6.5) * CFrame.Angles(0, 0, math.rad(90))
Goals = { CFrame = targetCFrame }
else
local targetCFrame = tree.main.CFrame * CFrame.new(0.95, x, 6.575) * CFrame.Angles(0, 0, math.rad(90))
Goals = { CFrame = targetCFrame }
end
MovementTween = TweenService:Create(part, TweenInfoData, Goals)
MovementTween:Play()
end
MovementTween.Completed:Wait()
if chopping == true then
print("bad")
end
chopping = true
onChopping(tree)
print(tree, oldtree)
end
end
ClickDetector.MouseClick:Connect(function()
onClicked(ClickDetector.Parent)
end)
end
while true do
wait()
if Following == true and chopping == false then
for _, part in pairs(Parts) do
--print(part.Name)
if part.Name == "top" then
local targetCFrame = hrp.CFrame * CFrame.new(0, x + 2.844, 3.5)
Goals = { CFrame = targetCFrame }
elseif part.Name == "middle" then
local targetCFrame = hrp.CFrame * CFrame.new(0, x + 1.445, 3.5)
Goals = { CFrame = targetCFrame }
else
local targetCFrame = hrp.CFrame * CFrame.new(0, x + 0.419, 3.5)
Goals = { CFrame = targetCFrame }
end
MovementTween = TweenService:Create(part, TweenInfoData, Goals)
MovementTween:Play()
end
task.wait(0.15)
end
end
Thanks a lot for the help, I fell like I tried everything.