this is problematic because the block can be moved inside other blocks and is just annoying to the user.
this is the current script:
--handle building mechanics------------
-------------------------------------
local currentblock = nil
local plr = game.Players.LocalPlayer
local mouse = plr:GetMouse()
local increment = 4 --movement increment--
--convert a number to the right increment--
function filterinc(num)
local a = math.floor(num)
a = a-(a%increment)
return a
end
----------------------------------------
--reposition the current block--
function dragblock()
if currentblock then
mouse.TargetFilter = currentblock
currentblock:SetPrimaryPartCFrame(CFrame.new(filterinc(mouse.Hit.X,increment + (currentblock.PrimaryPart.Size.X/2)),filterinc(mouse.Hit.Y,increment) + (currentblock.PrimaryPart.Size.Y/2),filterinc(mouse.Hit.Z,increment)))
end
end
mouse.Move:Connect(function()
dragblock()
end)
---------------------------------
--create a new ghost block to drag based on what gui button was pressed--
function newblock(n)
if currentblock then
currentblock:Destroy()
currentblock = nil
end
local orig = game.ReplicatedStorage.Buildings:FindFirstChild(n)
if orig then
local copy = orig:Clone()
copy.Parent = game.Workspace
for _, p in pairs(copy:GetChildren()) do
if p:IsA("BasePart") then
p.Transparency = 0.4
end
end
currentblock = copy
end
end
script.Parent.ScrollingFrame.LightArmor.MouseButton1Click:Connect(function()
newblock("LightArmor")
end)
-------------------------------------------------------------------------
--------------------------------------
---------------------------------------
Try using Model:Move(V3) instead of Model. PrimaryPart.CFrame = :Move takes in to account other blocks and move it vertically if there’s a block in its new place.
I dont trust move() as if theres only 0.1 studs out of place it wont place it were it needs to go.
also, this does not solve the offset issue as it will still be out of place when the player moves their mouse.
Make sure model’s PrimaryPart is in the center of the model, SetPrimaryPartCFrame just moves the primarypart to the given cframe, while moving the rest of the parts in the model to its offset from the PrimaryPart.
it looks like the problem lies in the offset from the filterinc output, try changing the results to (x, y, z) + a number until it gets to be in the center
--handle building mechanics------------
-------------------------------------
local currentblock = nil
local plr = game.Players.LocalPlayer
local mouse = plr:GetMouse()
local increment = 4 --movement increment--
--convert a number to the right increment--
function filterinc(x,inc)
return math.floor((x / inc) + 0.5) * inc
end
----------------------------------------
--reposition the current block--
function dragblock()
if currentblock then
mouse.TargetFilter = currentblock
currentblock:SetPrimaryPartCFrame(CFrame.new(filterinc(mouse.Hit.X,4),filterinc(mouse.Hit.Y,4),filterinc(mouse.Hit.Z,4)))
end
end
mouse.Move:Connect(function()
dragblock()
end)
---------------------------------
--create a new ghost block to drag based on what gui button was pressed--
function newblock(n)
if currentblock then
currentblock:Destroy()
currentblock = nil
end
local orig = game.ReplicatedStorage.Buildings:FindFirstChild(n)
if orig then
local copy = orig:Clone()
copy.Parent = game.Workspace
for _, p in pairs(copy:GetChildren()) do
if p:IsA("BasePart") then
p.Transparency = 0.4
end
end
currentblock = copy
end
end
script.Parent.ScrollingFrame.LightArmor.MouseButton1Click:Connect(function()
newblock("LightArmor")
end)
-------------------------------------------------------------------------
--------------------------------------
---------------------------------------
I made some changes, and it does help a lot with the offset, I think I may have to do some collision detection though when it goes inside another block as this seems unavoidable.