Making the code you have better
Connecting events inside other event handlers is usually wrong. In this code, every time you click, you start another loop that waiting for Move and Button1Up events.
You refer to mouse.Target 5 times; just make a variable. (This also seems to be necessary in some strange situations because mouse.Target can be re-computed within a single frame)
dim should not be its own variable, it should be a part of target. The simplest way is to just make target nil whenever you don't have a target (since nil indicates absence this is very natural)
Don't use silly abbreviations like plr, just type the full word out, player. ROBLOX can help you autocomplete words and it's easier to read and type.
Don't use == false and == true, just use the plain boolean (with not if appropriate).
Clean code reads like well written prose
The key to writing good code is just writing exactly what you intend.
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local target = nil
local function playerIsNear(point)
assert(typeof(point) == "Vector3")
local character = player.Character
if not character then
return false
end
local root = character:FindFirstChild("HumanoidRootPart")
if not root then
return false
end
return (root.Position - point).magnitude < 10
end
function onClick()
local union = mouse.Target
if union then
if union:IsA("UnionOperation") and not union.Locked then
if playerIsNear(union.Position) then
target = {
part = union,
wasAnchored = union.Anchored,
}
union.Anchored = true
end
end
end
end
function onMove()
if target then
local pointing = mouse.Hit.p
target.part.CFrame = CFrame.new(pointing.x, target.part.Position.y, pointing.y)
end
end
function onRelease()
if target then
target.part.Anchored = target.wasAnchored
target = nil
end
end
mouse.Button1Down:Connect(onClick)
mouse.Button1Up:Connect(onRelease)
mouse.Move:Connect(onMove)
Notice how the comments say exactly what you want to do? Notice how similar the code is to the comments?
Making the change
You want to change
When the player moves the mouse,
move the target (if any) to where they are pointing
to be
When the player moves the mouse,
move the target (if any) to where they are pointing
IF THEY ARE POINTING TO SOMETHING NEAR
Well, just go to that part of the code and add it:
function onMove()
if target then
local pointing = mouse.Hit.p
if playerIsNear(pointing) then
target.part.CFrame = CFrame.new(pointing.x, target.part.Position.y, pointing.y)
end
end
end
Why is the code longer?
Your code is 31 lines (27 are not blank).
My code is 85 lines. But:
- 10 are blank lines (as opposed to 4 in yours)
- 28 are comments (as opposed to 0 in yours)
- 1 is an
assert
Which only leaves 46 lines.
- 6 of which address error conditions your code doesn't (in
playerIsNear)
4 comments
Thanks you for helping me resolve my issue. When testing out your script though, it seemed to not be able to move from the Y axis. So when dragging the brick, it would remain on the same Y coordinate.
That's how your original code was written, so I assumed you wanted that.
Why have lines 9 and 16 both different variables? Why not just check with one if statement w/o the variables, since they're not going to be used any other way (outside the function), and are used only for that purpose? Just wonderin'. e-e
To be clear and have short lines. IMO, it's confusing (and error-prone) to re-compute a value rather than save the value to a variable.