Unintentional floating blocks on placement system

(Video made by my friend)

So the blocks keep floating and not going on the floor, and when I click to place a block and I click again, it isnt working. any help?

script:

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

local PosX
local PosY
local PosZ
	
local Model
local Button = script.Parent.TextButton
	
local GridSize = 2

local CanStart = true
local isPlacing = nil
local CanPlace = nil

local function Snap()
	PosX = math.floor(Mouse.Hit.X / GridSize + 0.5) * GridSize
	PosY = 3
	PosY = math.floor(Mouse.Hit.Z / GridSize + 0.5) * GridSize
end

local function Placement()
	if isPlacing and CanPlace then
		local PlacedModel = Model:Clone()
		PlacedModel.Parent = workspace.PlacedObjects
		
		isPlacing = false
		CanPlace = false
		CanStart = true

	end
end

local function Movement()
	Mouse.TargetFilter = Model
	
	Snap()
	
	Model:SetPrimaryPartCFrame(CFrame.new(PosX, PosY, PosZ))
end


local function StartPlacementWoodBlock()
	if CanStart then
		Model = game:GetService("ReplicatedStorage").WoodBlock:Clone()
		Model.Parent = workspace
		isPlacing = true
		CanPlace = true
		CanStart = false
		
		Mouse.Move:Connect(Movement)
	end
end

Mouse.Button1Down:Connect(Placement)
Button.MouseButton1Click:Connect(StartPlacementWoodBlock)

Try specifying the Y value to a certain number instead of the hit y value of the mouse

The issue is you set PosX, then PosY, then PosY again instead of PosZ inside your Snap function.

1 Like