So i am making a block creation and breaking script but when the block places it is placed on the grid i have but it creates it like 1 block next to the mouse Here is the code:
local mouse = game:GetService('Players').LocalPlayer:GetMouse() local HotbarItems = {'Grass','Stone','Bricks','Sand','WoolRed','MoltenRock','Netherrack','Water','Lava','Gravel'} local ItemList = {'Grass','Dirt','Stone','Bedrock','DiamondOre','CopperOre','IronOre','EmeraldOre','GoldOre','RedstoneOre','MoltenRock','Bricks','Leaves','WoolDarkGray','WoolOrange','WoolGray','WoolBrown','WoolDarkGreen','WoolLightGreen','WoolGreen','WoolBlue','WoolLightBlue','WoolLightYellow','SpruceWood','OakWood','DarkOakWood','AcaciaWood','AcaciaWoodPlanks','DarkOakWoodPlanks','OakWoodPlanks','SpruceWoodPlanks','Concrete','Cobblestone','Sand','Gravel','Netherrack','WoolRed','Water','Lava'} local Selected = 1 mouse.Button1Down:connect(function() local target = mouse.Target for index, value in ipairs(ItemList) do if target.Name == value then target:Destroy() end end end) mouse.Button2Down:connect(function() local PlacedObjectIdentifier = HotbarItems[Selected] local ClonedBlock = game:GetService('Lighting'):FindFirstChild(PlacedObjectIdentifier):Clone() ClonedBlock.Parent = game:GetService('Workspace') local p=mouse.Hit.p local x,y,z = p.x,p.y,p.z ClonedBlock.CFrame = CFrame.new(x-x%4,y-y%4,z-z%4) end) mouse.KeyDown:connect(function(key) if key == '1' then Selected = 1 script.Parent.Selector:TweenPosition(UDim2.new(script.Parent.Slot1.Position)) elseif key == '2' then Selected = 2 script.Parent.Selector:TweenPosition(UDim2.new(script.Parent.Slot2.Position)) elseif key == '3' then Selected = 3 script.Parent.Selector:TweenPosition(UDim2.new(script.Parent.Slot3.Position)) elseif key == '4' then Selected = 4 script.Parent.Selector:TweenPosition(UDim2.new(script.Parent.Slot4.Position)) elseif key == '5' then Selected = 5 script.Parent.Selector:TweenPosition(UDim2.new(script.Parent.Slot5.Position)) elseif key == '6' then Selected = 6 script.Parent.Selector:TweenPosition(UDim2.new(script.Parent.Slot6.Position)) elseif key == '7' then Selected = 7 script.Parent.Selector:TweenPosition(UDim2.new(script.Parent.Slot7.Position)) elseif key == '8' then Selected = 8 script.Parent.Selector:TweenPosition(UDim2.new(script.Parent.Slot8.Position)) elseif key == '9' then Selected = 9 script.Parent.Selector:TweenPosition(UDim2.new(script.Parent.Slot9.Position)) elseif key == '0' then Selected = 10 script.Parent.Selector:TweenPosition(UDim2.new(script.Parent.Slot10.Position)) end end)
for a grid of 4 you should use math.floor(value/4)*4
local Position = --poshere (Current position of the part) xdif = math.floor(Position.X/4)*4 -- Grids positions X value (0,4,8,12,16 etc) ydif = math.floor(Position.Y/4)*4 -- Grids positions Y value (0,4,8,12,16 etc) zdif = math.floor(Position.Z/4)*4 -- Grids positions Z value (0,4,8,12,16 etc) local Grid4Position = Vector3.new(xdif,ydif,zdif) -- Input position (on line 1) On a grid of four.
EDIT:
If you'd input
local Position = Vector3.new(3.5,18,-7)
This would return you:
local Grid4Position = Vector3.new(0,16,-8)
EDIT2:
Implemented into your code:
mouse.Button2Down:connect(function() local PlacedObjectIdentifier = HotbarItems[Selected] local ClonedBlock = game:GetService('Lighting'):FindFirstChild(PlacedObjectIdentifier):Clone() ClonedBlock.Parent = game:GetService('Workspace') local p=mouse.Hit.p local x,y,z = p.x,p.y,p.z -- Added stuff xdif = math.floor(x/4)*4 -- Grids positions X value (0,4,8,12,16 etc) ydif = math.floor(y/4)*4 -- Grids positions Y value (0,4,8,12,16 etc) zdif = math.floor(z/4)*4 -- Grids positions Z value (0,4,8,12,16 etc) ClodeBlock.CFrame = CFrame.new(xdif,ydif,zdif) end)