Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

how can i snap an uneven size part to grid in my placement system?

long story short im creating a grid placement system with snapping into place, but it only snaps if the part size is even (for example 2x2x2). if its not even then it snaps between the grids. i have tried adding half a stud to the part so its in the right place but it didnt work well. here is the script and an image:

local module = {}

module.place = function(plr,item)
	local blacklist = {}
	for i,v in pairs(item.Parts:GetChildren()) do
		table.insert(blacklist,v)
	end
	local mouse = plr:GetMouse()
	local function snapToGrid(vector3,middle,folder)
		local x = math.floor(vector3.X+0.5)
		local y = math.floor(vector3.Y+0.5)
		local z = math.floor(vector3.Z+0.5)
		folder.Parts:MoveTo(middle+Vector3.new(-x,y + folder.Parts.PrimaryPart.Size.Y/2,-z))
	end
	local hitbox = item.Parts.PrimaryPart
	local RS = game:GetService("RunService")
	local maxHeight = 250
	local plot
	for i,v in pairs(game.Workspace.Plots:GetChildren()) do
		if v.Owner.Value == plr.Name then
			plot = v
			for i,b in pairs(v.BaseParts:GetChildren()) do
				b.Grid.Transparency = 0
			end
		end
	end
	item.Parent = workspace.Placing
	RS.RenderStepped:connect(function()
		local ray = Ray.new(mouse.UnitRay.Origin,mouse.UnitRay.Direction*1000)
		local hit, mousepos = game.Workspace:FindPartOnRayWithIgnoreList(ray, blacklist)
		local pos = plot.BaseParts.BasePart1.Position - mousepos
		if hit then
			snapToGrid(pos,plot.BaseParts.BasePart1.Position,item)
		end
		wait()
	end)
end

return module

http://prntscr.com/xihm1t

1 answer

That is just how grids work. The solution will always require some trade-off. Basically you have four options.

  1. Only make even sized parts.
  2. Only make odd sized parts, and redraw the grid half stud each way.
  3. Make the "BasePart" invisible. For even sized models put it in the middle. For odd sized models put it off-centre by half stud.
  4. Adjust the script to take into account "BasePart" size. Below is my attempt at just this, but I have no way to test it reliably.
local xAdjust = folder.Parts.PrimaryPart.Size.X % 2 -- the remainder of division
local yAdjust = folder.Parts.PrimaryPart.Size.Y / 2
local zAdjust = folder.Parts.PrimaryPart.Size.Z % 2

folder.Parts:MoveTo(middle+Vector3.new(-x + xAdjust,y + yAdjust, -z + zAdjust))

I hope this helps.

2 comments

0

didnt expect anyone to answer, im currently trying out a solution, if it doesnt work then im gonna try yours

0

nope didnt rly work

This discussion is preserved as it was. New answers, comments, and votes are closed.