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)
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)
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
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
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?