Attempt To Index a "nil" with Position

Hello Devs.
Im currently working on a catching system for a commission and I’ve ran into a serious problem. When I use my Find Football Function Module.FindFootball to find the football in the workspace. The Module.CatchMechanic returns me a error that it can’t find the Football. I ran the Module.FindFootball In other instances to find the football so you can go into ball camera and it works just find. Why does it do this?

Image:
image

Code:

function Module:FindFootball()
	if workspace:FindFirstChild('Football') then
		-- Finish Code --
		return workspace:FindFirstChild('Football')
	end
	for _,Plr in PlrService:GetPlayers() do
		-- Check --
		if not Plr.Character then continue end
		local Football = Plr.Character:FindFirstChild('Football')
		-- Check --
		if Football and Football:IsA('MeshPart') then
			return Football
		end
	end
end
function Module:CatchMechanic()
	-- Boolean {Check} --
	if CanCatch == true then
		CanCatch = false
		-- Constants {Football} --
		local Football = Module.FindFootball()
		if not Football then
			-- Middle Catching Only --
			RemoteEvent:FireServer({'NewAction','CatchingTrue'})
			AvatarAnimations.FootballAnims.CatchAnims.MiddleAnimation:Play()
			-- Wait Until {Stopped} --
			AvatarAnimations.FootballAnims.CatchAnims.MiddleAnimation.Stopped:Wait()
			RemoteEvent:FireServer({'NewAction','CatchingFalse'})
		else
			-- Check {Football.Parent}
			if Football then
				-- Update {Distances} --
				MidDistance = Module.GetDistance(HumanoidRootPart,Football)
				HighDistance = Module.GetDistance(Head,Football)
				LowLDistance = Module.GetDistance(LeftLeg,Football)
				LowRDistance = Module.GetDistance(RightLeg,Football)
				LeftDistance = Module.GetDistance(LeftArm,Football)
				RightDistance = Module.GetDistance(RightArm,Football)
				-- Fire Event --
				RemoteEvent:FireServer({'NewAction','CatchingTrue'})
				-- Select Which Animation Will Be Playing --
				local Distances = {MidDistance,HighDistance,LowLDistance,LowRDistance,LeftDistance,RightDistance}
				local Closest = nil
				-- For Statement --
				for I = 2,#Distances do
					if Distances[I] < Distances[I - 1] then
						-- Set Closest Value --
						Closest = Distances[I]
					end
				end
				-- Now Play Animations {Check Closest} --
				if Closest == MidDistance then
					AvatarAnimations.FootballAnims.CatchAnims.MiddleAnimation:Play()
				elseif Closest == HighDistance then
					AvatarAnimations.FootballAnims.CatchAnims.HighAnimation:Play()
				elseif Closest == LowLDistance then
					AvatarAnimations.FootballAnims.CatchAnims.LowAnimation:Play()
				elseif Closest == LowRDistance then
					AvatarAnimations.FootballAnims.CatchAnims.LowAnimation:Play()
				elseif Closest == LeftDistance then
					AvatarAnimations.FootballAnims.CatchAnims.LeftAnimation:Play()
				elseif Closest == RightDistance then
					AvatarAnimations.FootballAnims.CatchAnims.RightAnimation:Play()
				end
				-- Wait Then Fire Event --
				task.wait(0.75)
				RemoteEvent:FireServer({'NewAction','CatchingFalse'})
			end
		end
		task.wait(0.5)
		CanCatch = true
	end
end
1 Like

The error comes from my Module.GetDistance Function so the problem is that its not finding the football. But Im not sure why

MidDistance = Module.GetDistance(HumanoidRootPart,Football)

Error comes from this line right here

I don’t see any in the top script relating to “Module.GetDistance.”

By any chance, did you try:

-- try:
local Football = Module:FindFootball()

-- Instead of
local Football = Module.FindFootball()

All I changed was the . between module and the function to a : as you defined them in the module as :.

Get Distance doesn’t really result to the problem. It only errors it cause the distance of the 2nd object im trying to find it nil

function Module:GetDistance(Target1,Target2)
	local Dist1 = Vector3.new(Target1.Position.X,0,Target1.Position.Z).Magnitude
	local Dist2 = Vector3.new(Target2.Position.X,0,Target2.Position.Z).Magnitude
	-- Finish Code --
	return (Dist1 - Dist2).Magnitude
end

But this is the GetDistance Function

Either way I get the same error changing it to :

Can you explain this more? What do you mean by 2nd object? The football?

Did you try adding print statement to see what the assignments are in the script?

So when you call the GetDistance Function you need 2 OBJ’s that you want to use to get the distance of. Yes I have tried using prints and it just sends an error. Doesn’t print anything at all

So Module:GetDistance() is never called?

print the module, and see if the functions are in the table

It gets called here

Could you show the part where you call the humanoidrootpart

Its erroring the 2nd OBJ which is the Target2 Which is the football. It has nothing to do with the character

So the football is found in the bottom script. (function Module:CatchMechanic())
Before the error, put:

print("Football:", Football, typeof(Football))

In Module:GetDistance(Target1,Target2)

print("Target1:", Target1,typeof(Target1), "| Target2:", Target2 typeof(Target2)

Unrelated

Does the character assignments update, when the player respawns?

image
It prints football as a Instance. It didn’t print the other thing cause Target 2 cant be found

What is target1 __getdistance, when you print? If that errors, print target1 separately + what type it is. (typeof())

Also, check HumanoidRootPart.

If you are wondering, I am trying to find what each instance/assignment is in the stack.

It printed this
image

I found an issue. (or possibly the issue)
image

Line:

-- try:
return Vector3.new(Dist1 - Dist2).Magnitude
--instead:
return (Dist1 - Dist2).Magnitude

My test

function GetDistance(Target1,Target2)
	local Dist1 = Vector3.new(Target1.Position.X,0,Target1.Position.Z).Magnitude
	local Dist2 = Vector3.new(Target2.Position.X,0,Target2.Position.Z).Magnitude
	-- Finish Code --
	return Vector3.new(Dist1 - Dist2).Magnitude
end

warn(GetDistance(workspace.SpawnLocation,workspace.Baseplate))