Why are two of my parts spining when i press R

I am implementing a building mechanic in my game

local button = script.Parent
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local RS = game.ReplicatedStorage
local Wood = RS.wood
local UIS = game:GetService("UserInputService")

local movement

button.MouseButton1Click:Connect(function()
	local wood = Wood:Clone()
	wood.Parent = game.Workspace
	movement = mouse.Move:Connect(function()
		wood.Anchored = true
		wood.Position = mouse.Hit.Position + Vector3.new(0, wood.Size.Y/2, 0)
		mouse.TargetFilter = wood
		local Highlight = Instance.new("Highlight")
		Highlight.FillColor = Color3.new(0, 255, 0)
		Highlight.Parent = wood
		
		mouse.Button1Up:Connect(function()
			movement:Disconnect()
			Highlight:Destroy()
		end)
		UIS.InputBegan:Connect(function(input, gameProcessedEvent)
			if input.KeyCode == Enum.KeyCode.R then
				local turns = 0
				turns += 15
				local newCframe = CFrame.Angles(0,math.rad(turns),0)
				wood.CFrame = wood.CFrame * newCframe
			end
		end)
	end)
end)

I am using cframe to turn the part (aka “the wood”) around but once I place down a part, and add a second of the same type, and press R to rotate, both of them are rotating when I would like for only one to do so. Any solutions?

When you’re cleaning up the connections (eg. in your Button1Up connection), you are never disconnecting the .InputBegan connection.

That dosent seem to work, any other ideas?

Can you show how you’re disconnecting the InputBegan connection?

sure this is it:

local button = script.Parent
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local RS = game.ReplicatedStorage
local Wood = RS.wood
local UIS = game:GetService("UserInputService")

local movement
local rotate

button.MouseButton1Click:Connect(function()
	local wood = Wood:Clone()
	wood.Parent = game.Workspace
	movement = mouse.Move:Connect(function()
		wood.Anchored = true
		wood.Position = mouse.Hit.Position + Vector3.new(0, wood.Size.Y/2, 0)
		mouse.TargetFilter = wood
		local Highlight = Instance.new("Highlight")
		Highlight.FillColor = Color3.new(0, 255, 0)
		Highlight.Parent = wood

		rotate = UIS.InputBegan:Connect(function(input, gameProcessedEvent)
			if input.KeyCode == Enum.KeyCode.R then
				local turns = 0
				turns += 15
				local newCframe = CFrame.Angles(0,math.rad(turns),0)
				wood.CFrame = wood.CFrame * newCframe
			end
		end)
		
		mouse.Button1Up:Connect(function()
			movement:Disconnect()
			rotate:Disconnect()
			Highlight:Destroy()
		end)
	end)
end)

How about instead of working with 3-level nested connections you try to make it more shallow with two levels because I think you’re creating way too many connections, creating two connections each time you move the mouse and you aren’t disconnecting them each time they’re made so you might have a memory leak as it’s difficult to keep up with all these connections. Try this:

button.MouseButton1Click:Connect(function()
	local wood = Wood:Clone()
	wood.Parent = workspace
    local movement, rotate, up
    movement = mouse.Move:Connect(function()
		wood.Anchored = true
		wood.Position = mouse.Hit.Position + Vector3.new(0, wood.Size.Y/2, 0)
		mouse.TargetFilter = wood
		local Highlight = Instance.new("Highlight")
		Highlight.FillColor = Color3.new(0, 255, 0)
		Highlight.Parent = wood
    end)
	rotate = UIS.InputBegan:Connect(function(input, gameProcessedEvent)
		if input.KeyCode == Enum.KeyCode.R then
			local turns = 0
			turns += 15
			local newCframe = CFrame.Angles(0,math.rad(turns),0)
			wood.CFrame = wood.CFrame * newCframe
		end
	end)
    up = mouse.MouseButton1Up:Connect(function()
        movement:Disconnect()
        rotate:Disconnect()
        up:Disconnect()
    end)
end)

It gives 2 errors

this one:

MouseButton1Up is not a valid member of PlayerMouse “Instance”

and this one:

‘Players.TheEllaGames.PlayerGui.ScreenGui.Frame.Wood.LocalScript’, Line 31

It also seemed to stop placing down blocks when I used the code you sent me.

Oh yeah that’s my bad, replace mouse.MouseButton1Up with mouse.Button1Up