- What do you want to achieve?
I’m trying to make a placement system similar to 🛒Retail Tycoon 2 - Roblox. More importantly the click and drag part of the placement system (shown below)
Watch showcase | Streamable
- What is the issue?
This video explains it well:
I don’t really know how to make this which results in stuff like this happening
This is the part when you start dragging your mouse in RenderStepped:
if holdingMouse1 and currentlyPlacing then
pos2 = lastTapPosition or mouse.Hit.Position
pos2 = placement:CalcPlacementCFrame(currentlyPlacing, pos2, rotation).Position
local startX,endX = math.min(pos1.X, pos2.X), math.max(pos1.X, pos2.X)
local startZ,endZ = math.min(pos1.Z, pos2.Z), math.max(pos1.Z, pos2.Z)
for i=startX, endX, placement.GridUnit do
for v=startZ,endZ, placement.GridUnit do
local cf = placement:CalcPlacementCFrame(currentlyPlacing, Vector3.new(i, 0, v), rotation)
if table.find(visuallyGridsTaken, cf.Position) then continue end
table.insert(visuallyGridsTaken, cf.Position)
local visual = visualPlacement:Clone()
visual:PivotTo(cf)
visual.Parent = workspace.Temp.Build
if placement:isColliding(visual) then
visual:Destroy()
else
table.insert(visualPlacements, visual)
end
end
end
end
pos1
is the mouse hit position when the player starts dragging (holding down mouse1)
pos2
is constantly being updated to the current mouse hit position
Both positions are centered on a grid tile
This is the whole code which deletes objects that aren’t inside the area between the 2 points pos1
and pos2
(visual
is the object/furniture the player is placing and visualPlacements
are the objects made when dragging)
mouseMovedConnection = mouse.Move:Connect(function()
for i,visual in visualPlacements do
if i==1 then continue end
local pos = visual:GetPivot().Position
local range = (pos2-pos1).Unit:Dot(pos-pos1) / (pos2-pos1).Magnitude
local isBetween = range >= 0 and range <= 1
if not isBetween then
table.remove(visualPlacements, i)
visual:Destroy()
end
end
end)
- What solutions have you tried so far?
I’ve tried using workspace:GetPartBoundsInBox()
and workspace:FindPartsInRegion3()
but they both failed most likely as I said that I’m not experienced into this type of stuff
I’m not asking for a script rewrite just how should I do it, I would also appreciate any tips on how to optimize this.