Problems with floor/object placement similar to Retail Tycoon 2

  1. 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

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

2 Likes

my bad streamable links aren’t working well
edit: can’t upload second video so you’ll just have to click the link :pensive:

1 Like

have you tried printing like theyre breakpoints?

the problem is mainly caused by the 2nd code cause it deletes things that it shouldn’t, there are also issues with the first code where for example i’m placing and dragging an 8x4 stud object which I didn’t show in the video (the grid size is 4 btw) and it sometimes has objects which aren’t aligned which I suspect is caused by how I generate positions (look at the picture)

nvm, fixed it myself

characters awlajslkjalkjlaal

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.