I Need Help With A Grid Placement System!

Like it says in the title, I need help with a grid placement system. I want the grid size to be 5 by 5, and I want it to be placed on a floor part.

Thank you so much!

11 Likes

Hey, I’m Hot_Coder and i will be helping you with what you need,

Simply you need to create a function that is similar to math.round() but you will actually make it 5 points rounding not 1 as the math.round() function does

so the function would be something like that

local gridSpacing = 5 -- Its five as you said

function getRoundedPosition(positionToRound)
	local posX = positionToRound.X
	local posY = positionToRound.Y
	local posZ = positionToRound.Z
	
	local roundedX = math.floor((posX / gridSpacing) + 0.5) * gridSpacing
	local roundedZ = math.floor((posZ / gridSpacing) + 0.5) * gridSpacing
	return Vector3.new(roundedX, posY, roundedZ)
end

local currentPartPosition = Vector3.new(47.56, 10, 22.49)

local roundedPos = getRoundedPosition(currentPartPosition)

print(roundedPos)

--Output 50, 10, 20

And for the Floor Thing you need a function that checks if the Part is being placed on the other part and not outside the part so inside of it heres the function to do that its very simple

local function isPartInside(part1, part2)
    local size1 = part1.Size
    local size2 = part2.Size

    local min1 = part1.Position - size1/2
    local max1 = part1.Position + size1/2
    local min2 = part2.Position - size2/2
    local max2 = part2.Position + size2/2

    return (min1.X >= min2.X) and (max1.X <= max2.X) and
           (min1.Y >= min2.Y) and (max1.Y <= max2.Y) and
           (min1.Z >= min2.Z) and (max1.Z <= max2.Z)
end
12 Likes

Thank you for that, rounding it was the big thing that I did not know how to do.

7 Likes

You are welcome you can now have my post as solution so it could help more people.

7 Likes

Sorry, but how do I make it only work on a certain part(Floor)

6 Likes

You want to make the Part or what ever you are building not be into the ground you want it to be on top of ground ?

6 Likes

So you want to be only be able to place on a specific Part then you would check for mouse.hit (not the best option)

or you would do hard math and get the part’s current X position - the part’s X size divided by 2 and check if the result rounded position is more than that then they are building out of the platform but you will have to do that for all the X and Z axis

5 Likes

On top of a floor piece, Right now it can go on anything.

5 Likes

Read my previous responses and tell me which one you mean

3 Likes

a specific part, I have a floor part that I want it to be placed on

4 Likes

I figured it out, I just set a filter on a few parts, and added a max position.
Thank you for your help, it is working how I want it now.

2 Likes

I have posted the best way to do that i have edited the solution message check it out

2 Likes

Thanks, I have it working, but I will change it to that.

2 Likes

Alright and i think you have to Rotate the part the Right way since its Orientation Sensitive so make sure you do that or else it might not work the way you wanted it to work

2 Likes

Got it, thank you for all your help.

2 Likes

I’m glad that i helped you, You are welcome.

2 Likes

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