local Players = game:GetService("Players")
local UserInputService = game:GetService("UserInputService")
local log = game.ReplicatedStorage.Log
local character = Players.LocalPlayer.Character
local debounce = false
local swinging = false
local chop1 = Instance.new("Sound")
chop1.SoundId = "rbxassetid://159798328"
local woodBreak = Instance.new("Sound")
woodBreak.SoundId = "rbxassetid://4988580646"
UserInputService.InputBegan:Connect(function(input, gameProcessed)
if gameProcessed or debounce then return end
if input.UserInputType == Enum.UserInputType.MouseButton1 then
local pickaxeSwing = character.Humanoid:LoadAnimation(script.AxeSwing) or nil
if character:FindFirstChild("Axe") then
debounce = true
swinging = true
if swinging then
character:FindFirstChild("Axe").Touched:Connect(function(hit)
if not swinging then return end
swinging = false
if hit.Name == "Stump" then
local hits = hit:FindFirstChild("Hits")
if hits.Value == 3 then
local logClone = log:Clone()
log.Anchored = false
log.CanCollide = true
log.Parent = workspace
log.Position = hit.Position
local woodBreakClone = woodBreak:Clone()
woodBreakClone:Clone()
woodBreakClone.Parent = hit
woodBreakClone.PlayOnRemove = true
woodBreakClone:Play()
woodBreakClone:Destroy()
hit.CanCollide = false
hit.Parent:FindFirstChild("Leaf"):Destroy()
hit:Destroy()
wait(.4)
log.Anchored = true
else
local chop1Clone = chop1:Clone()
chop1Clone.Parent = hit
chop1Clone:Play()
hits.Value = hits.Value + 1
end
end
end)
end
local axeSwing = character.Humanoid:LoadAnimation(script.AxeSwing)
axeSwing:Play()
character.Humanoid.WalkSpeed = 6
axeSwing.Stopped:Wait()
character.Humanoid.WalkSpeed = 16
debounce = false
swinging = false
end
end
end)
Did you watch the video? What happens is when you chop a tree down a log gets cloned from Replicated Storage, when you chop down another tree, the original log from the first tree teleports to the second.
You defined logClone as a clone of log from ReplicatedStorage, but you never use it. You use log, so you’re just using the same log over and over again. You could’ve easily figured this typo out by yourself.
Also: you make a hit connection on the axe ever time the player clicks and you never disconnect it. Make sure to disconnect uneeded connections or you’ll be leaking memory.
Interesting, I never learned how to disconnect events. Could you provide some sample code on disconnecting it. (Use UserInputsService and Touched to show me.)
You don’t need to disconnect your UserInputService connection because you’ll never not be needing it. You do need to disconnect the touched connection after it’s been used, because you only need it once every time the player clicks.
Ideally you should make a new thread for this however just as a quick example:
local RunService = game:GetService("RunService")
local End = false
local Event = RunService.Heartbeat:Connect(function()
if math.random(1,10) == 1 then
Event:Disconnect()
end
end)
This code above will work however Event will have a red line underneath, to avoid this simply predefine Event.