How would I create a grid system for my part? (look below)

Hello, I am @kaykay101wally and am looking how to solve my problem. I have already created a system that creates a placeholder part that appears at the mouse cursor (using a Raycast). This only happens if the hoeTool is equipped and mouse.Target is “TillSpot”.

local hoeTool = script.Parent
local Players = game:GetService("Players")
local localPlayer = Players.LocalPlayer

local RS = game:GetService("ReplicatedStorage")
local RunService = game:GetService("RunService")
local UIS = game:GetService("UserInputService")

local PlaceholderDirt = RS.PlaceholderParts.PlaceholderDirt
local storedDirt = RS.Dirt
local tillSpot = workspace:WaitForChild("TillSpot")

local mouse = localPlayer:GetMouse()
local equipped = false
local hoveringOverTillspot = false

print("initialized TillingManager")
wait(1)

local function tillSpot()
	local target = mouse.Target
	if target and target.Name == "TillSpot"  and equipped == true then
		hoveringOverTillspot = true
		local raycastParams = RaycastParams.new()
		raycastParams.FilterType = Enum.RaycastFilterType.Exclude
		raycastParams.FilterDescendantsInstances = {mouse.TargetFilter}

		local mouseRay = Ray.new(mouse.UnitRay.Origin, mouse.UnitRay.Direction * 1000)
		local mouseRayHitPart, mouseRayHitPosition = workspace:FindPartOnRayWithIgnoreList(mouseRay, {mouse.TargetFilter})
		local placeholderClone = PlaceholderDirt:Clone()
		placeholderClone.Parent = workspace.TillSpot
		placeholderClone.Position = mouseRayHitPosition
		wait(0.0001)
		placeholderClone:Destroy()
	else
		print("arguments not matched, wrong target or unequipped during a RunStep")
		hoveringOverTillspot = false
	end
end

hoeTool.Equipped:Connect(function()
	equipped = true
end)

hoeTool.Unequipped:Connect(function()
	equipped = false
end)

RunService.RenderStepped:Connect(tillSpot)

mouse.Button1Down:Connect(function()
	local hit = mouse.Target
	if hoveringOverTillspot then
		local newDirt = storedDirt:Clone()
		newDirt.Parent = workspace.TillSpot
		newDirt.Position = workspace.TillSpot:WaitForChild("PlaceholderDirt").Position
	end
end)

I need to create a grid placement in increments of 5.5, my dirt part’s size is (5.5,1,5.5)
How would I do this? I know it involves rounding

1 Like

You could use this formula:

grid = 5 -- studs
gridPosition = math.floor(posOfPart/grid)*grid

I tried putting it In like this:

local grid = 5.5
placeholderClone.Position = math.floor(placeholderClone.Position/grid)*grid

But that doesn’t work. Is that what you mean?

local grid = 5.5
placeholderClone.Position = math.floor((placeholderClone.Position/grid) + .5)*grid

I tried it like this:

local grid = 5.5
placeholderClone.Position = math.floor((placeholderClone.Position/grid) + .5)*grid

But it gives this error

Do I have to do the X Y and Z separately?

local grid = 5.5
placeholderClone.Position = Vector3.new(math.floor((placeholderClone.Position.X/grid) + .5)*grid, math.floor((placeholderClone.Position.Y/grid) + .5)*grid, math.floor((placeholderClone.Position.Z/grid) + .5)*grid)

It should work.

Thanks for the help! I had to change a couple things around, but this works!
Here’s the final code if anyone wants to see:

local grid = your grid increment here
placeholderClone.Position = Vector3.new(math.floor((mouseRayHitPosition.X/grid) + .5)*grid, mouseRayHitPosition.Y, math.floor((mouseRayHitPosition.Z/grid) + .5)*grid)
1 Like

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