Trying to get the nearest object

I am trying to get the nearest point but I get this error: Workspace.xure1.CalculateGrapplingHook:19: attempt to index nil with ‘GetChildren’ on line 23.
I haven’t tried another method because I don’t know why it would say that hook is nil.

Code:

local plr = game.Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()
local mouse = plr:GetMouse()

local function getNearestHook()
	local nearest = 0
	local nearestHook = nil
	
	for _, hook in pairs(workspace.Hooks:GetChildren()) do
		if (char.PrimaryPart.Position - hook.PrimaryPart.Position).Magnitude < nearest then
			nearest = (char.PrimaryPart.Positon - hook.PrimaryPart.Position).Magnitude
			nearestHook = hook.PrimaryPart
		end
	end
	
	return nearestHook
end

local function getNearestPoint(hook)
	local nearest = 0
	local nearestPoint = nil
	
	for _, point in pairs(hook:GetChildren()) do
		if (char.PrimaryPart.Position - point.Position).Magnitude < nearest then
			nearest = (char.PrimaryPart.Positon - point.Position).Magnitude
			nearestPoint = point
		end
	end
	
	return nearest, nearestPoint
end

mouse.Button1Down:Connect(function()
	local hook = getNearestHook()
	local nearest, nearestPoint = getNearestPoint(hook)
	
	if nearest <= 7 and nearestPoint then
		game:GetService("ReplicatedStorage").Events:WaitForChild("GrappleEvent")
	end
end)
1 Like

In the getNearestHook function, you are checking if the distance between the character and the hook is less than 0.

Thank you but I still have the same error!

If I try to get the name of the hook it say: attempt to index nil with ‘Name’.

Can you try printing out what the hook variable is supposed to be?

It’s supposed to be a part that you will move forwards to.

It is because getNearestPoint will always return nil. This line:

The magnitude will always be greater than 0, so that if statement will always be false, therefore the nearestPoint will stay nil.

To fix this, replace

local nearest = 0

--Change to this:
local nearest = 999999

That’s what @Pokemoncraft5290 said. The current code is

local plr = game.Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()
local mouse = plr:GetMouse()

local function getNearestHook()
	local nearest = 0
	local nearestHook = nil
	
	for _, hook in pairs(workspace.Hooks:GetChildren()) do
		if not nearest == nil then
			if (char.PrimaryPart.Position - hook.PrimaryPart.Position).Magnitude < nearest then
				nearest = (char.PrimaryPart.Position - hook.PrimaryPart.Position).Magnitude
				nearestHook = hook.PrimaryPart
			end
		else
			nearest = (char.PrimaryPart.Position - hook.PrimaryPart.Position).Magnitude
		end
	end
	
	return nearestHook
end

local function getNearestPoint(hook)
	local nearest = 0
	local nearestPoint = nil
	
	for _, point in pairs(hook:GetChildren()) do
		if (char.PrimaryPart.Position - point.Position).Magnitude < nearest then
			nearest = (char.PrimaryPart.Positoin - point.Position).Magnitude
			nearestPoint = point
		end
	end
	
	return nearest, nearestPoint
end

mouse.Button1Down:Connect(function()
	local hook = getNearestHook()
	local nearest, nearestPoint = getNearestPoint(hook)
	
	if nearest <= 7 and nearestPoint then
		game:GetService("ReplicatedStorage").Events:WaitForChild("GrappleEvent")
	end
end)
1 Like

I forgot to update the nearestHook variable!

Now the it prints the hook but I get an error when I print the point’s name.

What’s the error message? And you mean the getNearestHook function returns the correct hook, but the getNearestPoint function isn’t working?

Yeah, the error is

attempt to index nil with 'Name'  -  Client - CalculateGrapplingHook:31

Function:

local function getNearestPoint(hook)
	local nearest = 999999
	local nearestPoint = nil
	
	for _, point in pairs(hook:GetChildren()) do
		if (char.PrimaryPart.Position - point.Position).Magnitude < nearest then
			nearest = (char.PrimaryPart.Positoin - point.Position).Magnitude
			nearestPoint = point
		end
	end
	
	print(nearestPoint.Name)
	return nearest, nearestPoint
end

You included the print statement outside the GetChildren()) loop, resulting in that error

local function getNearestPoint(hook)
	local nearest = 999999
	local nearestPoint = nil
	
	for _, point in pairs(hook:GetChildren()) do
		if (char.PrimaryPart.Position - point.Position).Magnitude < nearest then
			nearest = (char.PrimaryPart.Positoin - point.Position).Magnitude
			nearestPoint = point
            print(nearestPoint.Name)
		end
	end
	
	return nearest, nearestPoint
end

Try this instead

Now it doesn’t print anything.

And the hook has children? Could you add the following prints and see what it says:

local function getNearestPoint(hook)
	local nearest = 999999
	local nearestPoint = nil
	
	for _, point in pairs(hook:GetChildren()) do
        print(nearest, (char.PrimaryPart.Position - point.Position).Magnitude)
		if (char.PrimaryPart.Position - point.Position).Magnitude < nearest then
            print("Made it past the if statement")
			nearest = (char.PrimaryPart.Positoin - point.Position).Magnitude
			nearestPoint = point
		end
	end
	
	print(nearestPoint.Name)
	return nearest, nearestPoint
end

It still doesn’t print anything.

Does the hook even have children?

image

If it isn’t printing anything then it isn’t registering that the hook has children. If you printed the following, does it print an empty table or does or print a list of the children?

print(hook:GetChildren())