Model isnt positioning correctly when moved?

Im creating a building system were players will move 4x4 models around with their mouse with 4 studs as the increment.

however the model isn’t staying with the mouse when it snaps to a correct position:

robloxapp-20200808-1115563.wmv (1005.4 KB)

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)
-------------------------------------------------------------------------


--------------------------------------
---------------------------------------
3 Likes

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.

1 Like

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.

1 Like

Just try it. If it doesn’t work, add a check for to see if another block in its place and move it up 4 studs if there is.

1 Like

collisions are eliminated, but its still not were the mouse is at, isn’t there anything I can do to polish this?

1 Like

Maybe check the position of mouse ray to the block and either not show the block or make it transparent to an extent.

1 Like

ill leave this topic open for a bit, and if no other options come up ill just go ahead and mark your answer as the solution.

1 Like

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.

1 Like

the primarypart for the model seen in the video is just that one grey part, its exactly 4x4 studs in size.

1 Like

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.