Setting block to position of result.position very glitchy

Im trying to make a block placement system and currently, when setting the block’s position it has a bit of a seizure

Here is the code (Local script)

--Services
local userinputservice = game:GetService("UserInputService")

--Variables
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local camera = workspace.CurrentCamera

local gridSize = 2
local range = 50

local isInBuildMode = true
local blockCanBePlaced = true

local params = RaycastParams.new()
params.IgnoreWater = true

--Instances
local Block = Instance.new("Part")
Block.Size = Vector3.new(4,4,4)
Block.Transparency = 0.5
Block.CanCollide = false

local previewBlock = Block:Clone()
previewBlock.Parent = workspace

--Events
local brickPlacedEvent = game.ReplicatedStorage:WaitForChild("BrickPlacedEvent")

--Main Functions

while isInBuildMode do
	local mouseToWorldRay = camera:ScreenPointToRay(mouse.X, mouse.Y, 0)
	local mouseToBlockResult = workspace:Raycast(mouseToWorldRay.Origin, mouseToWorldRay.Direction * range, params)
	
	if mouseToBlockResult then
		blockCanBePlaced = true
		previewBlock.Position = Vector3.new(
			math.floor(mouseToBlockResult.Position.X / gridSize) * gridSize, 
			math.floor(mouseToBlockResult.Position.Y) + (previewBlock.Size.Y / 2),
			math.floor(mouseToBlockResult.Position.Z / gridSize) * gridSize
		)
		
		previewBlock.BrickColor = BrickColor.new("Medium stone grey")
		
	else
		blockCanBePlaced = false

		previewBlock.BrickColor = BrickColor.new("Really red")
		
	end
	
	task.wait()
end

I have been stuck on this issue for quite some time now so any help would be appreciated!

Turn off “CanQuery” inside of the block

local previewBlock = Block:Clone()
previewBlock.Parent = workspace
previewBlock.CanQuery = false
1 Like

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