Passing parameters through a function makes them nil

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

Can you add comments identifying where in your script you are referring to?

Yep, I added comments on where I was talking about, but it happens for all function calls.

Target and Target.Rotation are the ones that are nil?

Yes, that’s correct. I think it might be due to the weird connections I put in them.

Target is not an argument to ModeJanitor.Add, or to the anonymous function passed to MouseDrag.Connect. It is actually an Upvalue, which means it is not provided at the time your MouseDrag event fires, but is bound (provided) at the time ModeJanitor.Add is called and has whatever the current values are at that point, which is the moment the InputBegan event at the top of the script is fired.
You actually have a very high order function here and you might be getting confused about what is happening when. Could you explain what you expect these values to be? I’m also not familiar with what ModeJanitor does.

Of course, I want to pass the mouse target and the rotation of that part to the function, also ModeJanitor is just an object of the Janitor class which helps me disconnect the connection later on.

I think you have an error in your parentheses actually. In lua if you forget arguments they are bound to nil.
This:

TargetModel:PivotTo(
	bounds(
		(initialPartCFrame * CFrame.new(Vector3.fromNormalId(face) * distance)) ) , 
	Target.Rotation, 
	Target
)

Looks like it was meant to be this:

TargetModel:PivotTo(
	bounds(
		initialPartCFrame * CFrame.new(Vector3.fromNormalId(face) * distance), 
		Target.Rotation, 
		Target
	)
)

That is you accidentally passed 3 arguments to PivotTo() and not 3 arguments to bounds()

im so stupid lol :rofl:

thanks for that, it worked

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.