Grid Placement System

I’m trying to make a placement system, but i dont know how implement a grid into it, because of the orientation of the blocks. I also see that some of the blocks dont rotate when the mouse goes on it, because its in a model. any help?

Heres the script:

local RunService = game:GetService("RunService")
local UIS = game:GetService("UserInputService")

local Player = game.Players.LocalPlayer
local Char = Player.Character or Player.CharacterAdded:Wait()
local Mouse = Player:GetMouse()

local PosX
local PosY
local PosZ
local Rot = 0
 
local Model
local Button = game.Players.LocalPlayer.PlayerGui.ScreenGui.MainGui.GSOoneblockbut
local GridSize = 2

local Fil = script.Parent.Parent.Parent.Filters
local Fact = script.Parent.Parent.Parent.Factions
local But = script.Parent.Parent.Parent.Buttus
local scroll2 = script.Parent.Parent.Parent.MainGui_12
local scroll = script.Parent.Parent.Parent.MainGui
local Keyz = script.Parent.Parent.Parent.Keyz
local Keyzz = script.Parent.Parent.Parent.Keyzz
local Keyzzz = script.Parent.Parent.Parent.Keyzzz

local CanStart = true
local IsPlacing = nil
local CanPlace = nil

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

local function Placement()
 if IsPlacing and CanPlace then
  local PlacedModel = Model:Clone()
  PlacedModel.GSOoneblock.CanCollide = true
  PlacedModel.GSOoneblock.Anchored = false
  PlacedModel.Parent = workspace.PlacedObjects
  game.StarterGui.ScreenGui.Values.GSOoneblock.Value = game.StarterGui.ScreenGui.Values.GSOoneblock.Value -1
 end
end

local function Movement()
 if IsPlacing and CanPlace and not CanStart then
 Mouse.TargetFilter = Model
   
  Snap()
   
  Model:SetPrimaryPartCFrame(CFrame.new(PosX, PosY, PosZ) * CFrame.Angles(-math.rad(Mouse.Target.Orientation.x), math.rad(Mouse.Target.Orientation.y), math.rad(Mouse.Target.Orientation.z)))
else
  return Mouse.Hit.p
 end
end

local function StartPlacementCrate()
 if CanStart then
  scroll:TweenPosition(UDim2.new(-0.830, 0,0.221, 0),"Out","Quad",0.4)
  scroll2:TweenPosition(UDim2.new(-0.590,0,0.539,0),"Out","Quad",0.4)
  Fact:TweenPosition(UDim2.new(-1, 0,0.221,0),"Out","Quad",0.4)
  Fil:TweenPosition(UDim2.new(-1,0,0.221, 0),"Out","Quad",0.4)
  But:TweenPosition(UDim2.new(-1,0,0.13,0),"Out","Quad",0.4)
  Keyz:TweenPosition(UDim2.new(0.426, 0,0.773, 0),"Out","Quad",0.4)
  Keyzz:TweenPosition(UDim2.new(0.277, 0,0.787, 0),"Out","Quad",0.4)
  Keyzzz:TweenPosition(UDim2.new(0.567, 0,0.787, 0),"Out","Quad",0.4)
  Model = game:GetService("ReplicatedStorage").GSOoneblock:Clone()
  Model.Parent = workspace
  script.Parent:TweenPosition(UDim2.new(0.211, 0,0.180, 0),"Out","Quad",0)
  wait()
  script.Parent:Clone().Parent = Keyz
  IsPlacing = true
  CanPlace = true
  CanStart = false
  
  Mouse.Move:Connect(Movement)
 end
end

local function Rotation(input, GPE)
 if IsPlacing and CanPlace and not CanStart and input.KeyCode == Enum.KeyCode.Q then
  Rot = Rot + 90
  
  Model:SetPrimaryPartCFrame(Model.PrimaryPart.CFrame * CFrame.Angles(0, math.rad(Rot), 0))
 end
end

local function Rotation2(input, GPE)
 if IsPlacing and CanPlace and not CanStart and input.KeyCode == Enum.KeyCode.E then
  Rot = Rot - 90
  
  Model:SetPrimaryPartCFrame(Model.PrimaryPart.CFrame * CFrame.Angles(0, math.rad(Rot), 0))
 end
end
local function StopPlacement(input)
	if input.KeyCode == Enum.KeyCode.X then
		Keyz:TweenPosition(UDim2.new(0.426, 0,1, 0),"Out","Quad",0.4)
		Keyzz:TweenPosition(UDim2.new(0.277, 0,1, 0),"Out","Quad",0.4)
 		Keyzzz:TweenPosition(UDim2.new(0.567, 0,1, 0),"Out","Quad",0.4)
		
		IsPlacing = false
		CanPlace = false
		CanStart = true
		
		Model:Destroy()
		wait(0.5)
		Keyz:ClearAllChildren()
	end
end

local function LowBlocks(input)
	if game.StarterGui.ScreenGui.Values.GSOoneblock.Value <= 0 then
		local yeetele = Player.PlayerGui.ScreenGui.MainGui
		
		IsPlacing = false
		CanPlace = false
		CanStart = true
		
		yeetele.GSOoneblockbut.Visible = false
		Model:Destroy()
	else
		local yeetele = Player.PlayerGui.ScreenGui.MainGui
		yeetele.GSOoneblockbut.Visible = true
	end
end
Mouse.Button1Down:Connect(Placement)
Button.MouseButton1Click:Connect(StartPlacementCrate)
UIS.InputBegan:Connect(Rotation)
UIS.InputBegan:Connect(Rotation2)
UIS.InputBegan:Connect(StopPlacement)
UIS.InputBegan:Connect(LowBlocks)
UIS.InputEnded:Connect(Rotation)
UIS.InputEnded:Connect(Rotation2)
UIS.InputEnded:Connect(StopPlacement)
UIS.InputEnded:Connect(LowBlocks)

i am new to scripting and i made this from tutorials

2 Likes

Hello
There is already a tutorial here on the devforum which helped me solve your problem a while ago.
You can check it here.

6 Likes