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