So I'm coding this shark that's gonna look if a player is near and if they are the body velocity is gonna push to them, But my error says Workspace.Shark.MeshPart.MainScript:7: attempt to index nil with 'Position', Does anyone know what's happening or if I did something wrong, Thank you

Here's my code

local debris = game:GetService("Debris")
local bodyVelocity = script.Parent:FindFirstChild("BodyVelocity")
local killPart = script.Parent.Parent:FindFirstChild("Head")

for i, players in pairs(workspace:GetDescendants()) do
	if players:FindFirstChild("Humanoid") then
		local magnitude = (players:FindFirstChild("HumanoidRootPart").Position - script.Parent.Position).magnitude
		print("No player found yet")
		if magnitude <= 25 then
			while wait() do
				bodyVelocity.Velocity = players:FindFirstChild("HumanoidRootPart").Position
				print("Found player")
			end
		end
	end
end

1 comment

0

I see so many people doing this. do not put obj:FindFirstChild(name).property. ITS the same as obj.name.property, unless you use an if check