If I print the variables before I pass them through the variable it prints fine, but once I print the arguments in the function it becomes nil. Everything is in a local script.
Local Script:
UISConnection = UserInputService.InputBegan:Connect(function(input, gameprocessed)
if not gameprocessed and (input.UserInputType == Enum.UserInputType.MouseButton1 or input.UserInputType == Enum.UserInputType.Touch) and mouse.Target and mouse.Target:IsDescendantOf(plot) then
if os.clock() - startTime < 0.2 then
ModeJanitor:Cleanup()
local Target = mouse.Target
local TargetModel = Target:FindFirstAncestorWhichIsA("Model")
Target = TargetModel.PrimaryPart
if not Target then
TargetModel.PrimaryPart = TargetModel.RootPart
end
if Target and TargetModel then
local GUIs = playerGui:GetGuiObjectsAtPosition(mouse.X, mouse.Y)
Handle.Adornee = TargetModel.RootPart
print(GUIs)
print(Target)
print(TargetModel)
if #GUIs == 0 then
Seletcted.Parent = TargetModel
end
local initialPartCFrame
ModeJanitor:Add(
Handle.MouseButton1Down:Connect(function(face: Enum.NormalId)
initialPartCFrame = Target.CFrame -- save the part's CFrame
end)
)
--I call the function here
ModeJanitor:Add(
Handle.MouseDrag:Connect(function(face: Enum.NormalId, distance: number)
print(Target.Rotation)
print(Target)
TargetModel:PivotTo(bounds( (initialPartCFrame * CFrame.new(Vector3.fromNormalId(face) * distance))) , Target.Rotation, Target)
end)
)
--Ends here
ModeJanitor:Add(
Handle.MouseButton1Up:Connect(function(face : Enum.NormalId)
Remotes.InstanceRequest:FireServer(Target, bounds(Target.CFrame, Target.Rotation, Target))
end)
)
end
end
startTime = os.clock()
end
end)
Bounds Function:
local function bounds(c: CFrame, primaryRotation: Vector3, primary:BasePart) : CFrame
print(primaryRotation)
print(primary)
-- Calculate plot bounds in local space
local plotCFrame = plot.CFrame
local plotSize = plot.Size
-- Transform the input point to the plot's local space
local relativePos = plotCFrame:PointToObjectSpace(c.Position)
-- Account for primary part rotation
local primaryRotCFrame = CFrame.Angles(
math.rad(primaryRotation.X),
math.rad(primaryRotation.Y),
math.rad(primaryRotation.Z)
)
-- Calculate the rotated size
local primarySize = primary.Size
local rotatedSize = Vector3.new(
math.abs(primarySize.X * primaryRotCFrame.RightVector.X) + math.abs(primarySize.Z * primaryRotCFrame.LookVector.X),
primarySize.Y, -- Y rotation doesn't affect bounds
math.abs(primarySize.X * primaryRotCFrame.RightVector.Z) + math.abs(primarySize.Z * primaryRotCFrame.LookVector.Z)
)
-- Update bounds based on rotated size
local lowerX = -plotSize.X / 2 + rotatedSize.X / 2
local upperX = plotSize.X / 2 - rotatedSize.X / 2
local lowerZ = -plotSize.Z / 2 + rotatedSize.Z / 2
local upperZ = plotSize.Z / 2 - rotatedSize.Z / 2
-- Clamp the position in local space
local clampedX = math.clamp(relativePos.X, lowerX, upperX)
local clampedZ = math.clamp(relativePos.Z, lowerZ, upperZ)
-- Transform the clamped position back to world space
local clampedWorldPos = plotCFrame:PointToWorldSpace(Vector3.new(clampedX, relativePos.Y, clampedZ))
-- Return the new CFrame with the adjusted position
return CFrame.new(clampedWorldPos.X, c.Y, clampedWorldPos.Z)
end