I am trying to get a part to move with the players mouse but at the same time stay within the boundaries of the players base. To get the part to stay in the base I am getting the sides of the hitbox (the parts 'primary part') and the base. I then check if the right side of the hitbox is greater than the bases right side and if it is then set the x of hitbox's position to the right side of the base. I do that with all the other sides. What happens when the program runs is the part just stays in the left back corner and in my output I see 'left' and 'back'
--[[
x and z are by default the mouse x and z (this is not shown in this snippet)
--]]
-- Get sides of hitbox
local hSides = {
right = hitbox.Position.X + (hitbox.Size.X / 2);
left = hitbox.Position.X - (hitbox.Size.X / 2);
forward = hitbox.Position.Z + (hitbox.Size.Z / 2);
back = hitbox.Position.Z - (hitbox.Size.Z / 2);
}
-- Get sides of base
local bSides = {
right = base.Position.X + (base.Size.X / 2);
left = base.Position.X - (base.Size.X / 2);
forward = base.Position.Z + (base.Size.Z / 2);
back = base.Position.Z - (base.Size.Z / 2);
}
if hSides.right > bSides.right then
print("right")
x = bSides.right
elseif hSides.left < bSides.left then
print("left")
x = bSides.left
end
if hSides.forward > bSides.forward then
print("forward")
z = bSides.forward
elseif hSides.back < bSides.back then
print("back")
z = bSides.back
end
-- Set item position
local newPos = Vector3.new(x, (base.Position.Y + (base.Size.Y / 2)) + (hitbox.Size.Y / 2), z)
item:SetPrimaryPartCFrame(CFrame.new(newPos))
