Grid placement system not moving correctly

Basically, I made a script from a grid placement system tutorial, since I don’t know how to make this correctly. It works fine when the step is 1, however if I change it, the part doesn’t spawn at the mouse’s location, but rather somewhere 1000 studs away. What could be the issue?


local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()

local tweenservice = game:GetService("TweenService")
local info =, Enum.EasingStyle.Sine, Enum.EasingDirection.Out)

local Block = script:WaitForChild("Block")
Block.Anchored = true
Block.Orientation =,0,0)

local BlockGhost = Block:Clone()
BlockGhost.Parent = workspace
BlockGhost.Transparency = 0.5
BlockGhost.CanCollide = false

Mouse.TargetFilter = BlockGhost

local step = 4

	local MousePos = Mouse.Hit.Position
	local BlockPos =, Block.Size.Y / 2, 0)
	BlockPos += MousePos
	local XPos = math.round(BlockPos.X / step)
	local YPos = math.round(BlockPos.Y)
	local ZPos = math.round(BlockPos.Z / step)
	BlockPos =, YPos, ZPos)
	local tween = tweenservice:Create(BlockGhost, info, {Position = BlockPos}):Play()

	local NewBlock = Block:Clone()
	NewBlock.Parent = workspace.Blocks
	NewBlock.Position = BlockGhost.Position

Instead of adding the blockPos you can add a mouse filter.
Also adding the character to the filter is handy.

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
mouse.TargetFilter = {player.Character, block}

And for the gridded position of the block do this:

local XPos = math.round(BlockPos.X / step) * step
local YPos = math.round(BlockPos.Y / step) * step
local ZPos = math.round(BlockPos.Z / step) * step

You might have to adjust some things to your liking.