Building system

Alright so Too challenge myself I’m trying to make a building system and basically the way it’s supposed to work is when I click a gui a selection box spawn that I can move around and once I click a again the selection box disappears and a piece off wood, stone, etc spawn in the selection box’s position however I am having trouble with making this

Here’s the code:

local Players = game:GetService(“Players”)
local Player = game.Players.LocalPlayer
local Wood = game.Players.LocalPlayer.PlayerGui.ScreenGui.Frame.Wood
local Mouse = Player:GetMouse()
local Building = false
local ContextActionService = game:GetService(“ContextActionService”)
local Render = game:GetService(“RunService”)

local function Placement()

local x, y, z = Mouse.Hit.X, Mouse.Hit.Y, Mouse.Hit.Z
local wood = Instance.new("Part")
wood.Name = ("wood")
wood.Parent = game.Workspace
wood.Anchored = true
wood.Size = Vector3.new(18, 3, 3)
wood.Color =  Color3.fromRGB(106, 57, 9)
wood.Material = ("Wood")
wood.Position = Vector3.new(x, y + wood.Size.Y*0.5, z)

ContextActionService:UnbindAction("Placing", Placement, false, Enum.UserInputType.MouseButton1, Enum.UserInputType.MouseButton1)

end

Wood.MouseButton1Down:Connect(function()

local SelectionBox = Instance.new("Part")
SelectionBox.Parent = game.Workspace
SelectionBox.Name = ("SelectionBox")
SelectionBox.Size = Vector3.new(18, 3, 3)
SelectionBox.Material = ("Neon")
SelectionBox.Transparency = 0.7
SelectionBox.Color = Color3.fromRGB(0, 255, 0)
SelectionBox.Anchored = true
Mouse.TargetFilter = SelectionBox
local Connection = true

if Connection == true then
Render.RenderStepped:Connect(function()
local x, y, z = Mouse.Hit.X, Mouse.Hit.Y, Mouse.Hit.Z
SelectionBox.Position = Vector3.new(x, y + SelectionBox.Size.Y*0.5, z)
print(“Work”)
end)
end

ContextActionService:BindAction("Placing", Placement, false, Enum.UserInputType.MouseButton1)

if Enum.UserInputType.MouseButton1 then
	Connection = false
	SelectionBox:Destroy()
	
end	

end)

Try using UserInputService instead of GetMouse.

What exactly is the issue? What does/doesn’t work?

I can see something here:

This will instantly run because that logic statement checks if Enum.UserInputType.MouseButton1 exists (which it does) so it returns True instantly. This Boolean Logic statement causes the code inside the if statement to run immediately.

Thank you for mentioning that as that helps me alot to figure it out . Right now when I click the gui no selection box pops up but once I click again wood spawns exactly where my mouse is