Gird placement system not working on the Y axis?

Hello, so I have been working on a grid placement system this past week and keep having problems I have had some help from the devfourm but I am still having problems.
my problem is that move a block on the Y axis it doesnt snap to the grid and lets me go free for all with movment but when I go on X or Z it snaps back to grid.

I also keep having the problem where i can place a block inside another block and i dont know how to fix this.

2
I want it that if I try to place a block where a block already is the privew block turns red and doesnt let me place it.

Here is my script.

Local script.

local placementEvent = game.ReplicatedStorage.PlacementEvent
local ObjectFolder = game.ReplicatedStorage:WaitForChild("PlacementFolder")

local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local mouse = player:GetMouse()

local Frame = script.Parent:WaitForChild("Frame")
local UserImputService = game:GetService("UserInputService")
local RunService = game:GetService("RunService")



local PlacingObject = false
local RotatingObjectR = false
local RotatingObjectT = false
local RotatingObjectY = false
local RotatingObjectCoolDown = false 

for i, Button in pairs(Frame:GetChildren()) do
	
	if Button:IsA("TextButton") then
		
		Button.MouseButton1Click:Connect(function()
			
			if PlacingObject == false then
				
				PlacingObject = true
				Frame.Visible = true
				
				local RotatingAmount = 0
				local RotatingAmountT = 0
				local RotatingAmountY = 0
				
				local PrevieuwObject = ObjectFolder:FindFirstChild(Button.Name):Clone()
				PrevieuwObject.Parent = game.Workspace
				
				for i, Parts in pairs(PrevieuwObject:GetDescendants()) do
					
					if Parts:IsA("BasePart") then
						
						Parts.Transparency = 0.5
						Parts.CanCollide = false						
					end
				end
				
				UserImputService.InputBegan:Connect(function(Key, GameProcessed)
					
					if not GameProcessed then
						if not RotatingObjectCoolDown then
							if Key.KeyCode == Enum.KeyCode.R then

								RotatingObjectR = true

								while RotatingObjectR == true do
									wait()
									RotatingAmount += 90
									RotatingObjectCoolDown = true
									wait(0.1)
									RotatingObjectCoolDown = false
								end							
							end
						end						
					end					
				end)
				
				UserImputService.InputEnded:Connect(function(Key)
					
					if Key.KeyCode == Enum.KeyCode.R then
						
						RotatingObjectR = false
					end
				end)
				
				RunService.RenderStepped:Connect(function()
					
					if PlacingObject == true then
						
						mouse.TargetFilter = PrevieuwObject
						
						if PrevieuwObject:FindFirstChild("MainPart") then
							
							local x = math.floor(mouse.Hit.Position.X / 2) * 2
							local y = mouse.Hit.Position.Y + PrevieuwObject.PrimaryPart.Size.Y / 2 or math.floor(mouse.Hit.Position.Y / 2) * 2
							local z = math.floor(mouse.Hit.Position.Z / 2) * 2
							
							local ObjectCFrame = CFrame.new(x, y, z)
							local ObjectAngles = CFrame.Angles(0, math.rad(RotatingAmount), 0)
							
							PrevieuwObject:SetPrimaryPartCFrame(ObjectCFrame * ObjectAngles)
						end
					end			
				end)
				
				mouse.Button1Up:Connect(function()
					
					if PlacingObject == true then
						
						PlacingObject = false
						
						placementEvent:FireServer(PrevieuwObject.Name, PrevieuwObject.PrimaryPart.CFrame)
						
						Frame.Visible = true
						PrevieuwObject:Destroy()
					end
				end)	
			end		
		end)	
	end	
end

Server script

local PlacementEvent = game.ReplicatedStorage.PlacementEvent
local ObjectFolder = game.ReplicatedStorage:WaitForChild("PlacementFolder")

PlacementEvent.OnServerEvent:Connect(function(Player, PreviewObject, ObjectCFrame)
	
	local Object = ObjectFolder:FindFirstChild(PreviewObject):Clone()
	
	Object:SetPrimaryPartCFrame(ObjectCFrame)
	Object.Parent = game.Workspace
end)

Help is greatly wanted and thanks for reading.

1 Like

I’m not sure what you expect the or to do for the y-axis.
But how it is right now it won’t do the math.floor( /2) *2 for the y-axis.

Do you know how I fix it and make it work?

Depends on what you want this to do.

All I need it to do Is make that line do the same and make it when I move the preview block around it snaps to the grid on the Y axis.

That line at the moment keeps the block from not showing in the ground.

local hitPos = mouse.Hit.Position - mouse.Hit.LookVector * .01 -- or maybe even less

Then do the calculations for the x, y and z axis the same way but now with hitPos.


This will offset the hit pos in the direction of the camera, so that the point will be in the grid square instead of on the edge.

It works but now when I want to place the block its half in the ground.

Then you have to either move the ground down, so that it isn’t half on a grid tile.

Or move the grid by doing + or - some value when calculating y.

Could you apply you part to my script as I can’t seem to get it to work if it’s not too much.

either

hitPos += Vector3.yAxis * yourAmount

or

local y = math.floor(hitPos / 2) * 2 + yourAmount

I still can’t get it too work.

here is my new render function.

RunService.RenderStepped:Connect(function()
					
					if PlacingObject == true then
						
						mouse.TargetFilter = PrevieuwObject
						
						if PrevieuwObject:FindFirstChild("MainPart") then
							
							local hitPos = mouse.Hit.Position - mouse.Hit.LookVector * .01
							local y = math.floor(hitPos.Y / 2) * 2 + 2
							local x = math.floor(hitPos.X / 2) * 2
							local z = math.floor(hitPos.Z / 2) * 2
							
							local ObjectCFrame = CFrame.new(x, y, z)
							local ObjectAngles = CFrame.Angles(0, math.rad(RotatingAmount), 0)
							
							PrevieuwObject:SetPrimaryPartCFrame(ObjectCFrame * ObjectAngles)
						end
					end			
				end)

Could you show screenshots?

Also maybe try + mouse.Hit.LookVector instead of -

Also also maybe try

This is whats happing
Ground

Also how would I make it go red if its inside a block because I can do this.
(this is old photo)
2

I have tried moving the baseplate down too.

For checking if there are already parts there you can use :GetPartBoundsInBox (or a different shape), or just a raycast or two.


How much did you move the baseplate? It should be 2 studs like before.

Also have you tried hitPos += Vector3.yAxis * yourAmount

No sorry I havnt tried

As i dont know how to apply it.

put it inbetween these lines

(instead of doing +2 on the y)


Although I don’t think it will change anything actually

Sadly it didnt work do you think i should start from the beginning and try make it work?


Could you create a minimal reproducible example? So I can see what’s going on in studio?

Wdym by

I havnt heard that before