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

Why Is This Block Creating System Not Aligning To The Grid Properly?

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)

1 answer

✓ Accepted answer

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)

11 comments

0

Can you give a little explanation of what each piece does?

0

Also i realised that i wrote the version of the script that had the actual code commented out

0

i just fixed that

0

Last thing, how do i set the block's position to what this returns?

0

If this helped, Please accept and upvote this awnser. :D -Happy hollidays!

0

I cant upvote because i dont have 25 rep but i would if i could and also, how do i create the block next to or on top of another block if the player clicks on top or the side or under it?

0

Suggest making each block 4.001,4.001,4.001 wide, Thats what i did in my game.

0

ok, also i accidentally managed to mess up some unions and now they show as nothing, any way i can fix that?

0

Try seperating them and modeling them again

0

it came up with "Error 64-Bit Coding Could not be found" or something like that

0

It is also still out of Aim a bit check it out here: https://web.roblox.com/games/588166455/Universal-Worlds

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