Tree chopping script cloning incorrectly

The logs aren’t cloning the way I want them to.

Here’s a video: https://streamable.com/b1fqzn

Here’s the code:

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)
1 Like

Could you please provide an error code, or just the code which you receive?

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.

Mhm. I would like to see the error code, or just code you are receving. Go in game and press F12 or F9

There is no error code… It works, just doesn’t clone the way I want it to.

Alright. Sorry, I believe I am confused here. I will let another programmer respond, as I may not understand as well as they do.

Have a great day, stay safe
MysteriouslyMythical
Lead Programmer

It appears that you are using the variable log instead of log clone.

local woodBreakClone = woodBreak:Clone()
woodBreakClone:Clone()

The second line of this seems unnecessary.

That’s actually a sound effect, but yeah I do seem to use a lot of redundant stuff. :sweat_smile:

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.

3 Likes

You appear to be cloning the log but setting the original logs position.

local logClone = log:Clone()
...
log.Position = hit.Position

You should use logClone.Position instead (same goes with anchored, Parent etc.)

1 Like

Thanks, I over looked that, sorry for the trouble.

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.

In your example:

character:FindFirstChild("Axe").Touched:Connect(function(hit)

You could do something like this:

local connection
connection = character:FindFirstChild("Axe").Touched:Connect(function(hit)
     --stuff
     connection:Disconnect()
end)
1 Like

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.

Okay, thank you! I’ll make sure to implement that in my code. :+1:

1 Like

Thank you as well, I’ll take that into account as well for future code.