Help with raycast attempt to index nil with instance

local BlackList = {}
		for i,v in pairs(char:GetChildren()) do
			if v:IsA("BasePart") then
				table.insert(BlackList, v)
			end
		end


		local rayparams = RaycastParams.new()
		rayparams.FilterDescendantsInstances = {BlackList}
		rayparams.FilterType = Enum.RaycastFilterType.Blacklist

		for i,v in pairs(game.Players:GetPlayers()) do

			local direction = v.Character.HumanoidRootPart.Position - char.HumanoidRootPart.Position

			local NewRayResult = workspace:Raycast(char.HumanoidRootPart.Position, direction,rayparams)
			
			print(v.Character.Name)

			if NewRayResult.Instance then
				if NewRayResult.Instance:FindFirstChildOfClass("Humanoid") then
					if NewRayResult.Distance < 6 then
						NewRayResult.Instance:FindFirstChild("Humanoid"):TakeDamage(damage.Value)
						task.spawn(function()
							NewRayResult.Instance:FindFirstChild("Humanoid").WalkSpeed = 6
							task.wait(0.6)
							NewRayResult.Instance:FindFirstChild("Humanoid").WalkSpeed = 16
						end)
						return v
					end

				end
			else

				warn("Cannot find raycast direction")
			end
		end

this is my code i am getting the other players character by looping through the players but it returns the ā€œattempt to index nil with instanceā€ error does anybody know why?

v.Character is effectively just a pointer. Define the character as a variable, then get the HRP:

local character = v.Character
local direction = character.HumanoidRootPart.Position - char.HumanoidRootPart.Position
1 Like

for some reason it still errors

What is the exact error and which line does it say it is erroring on. Let us know.

1 Like

this is the line it errors on
,

Add a second check.

if NewRayResult and NewRayResult.Instance then
1 Like

The issue has been fixed thanks for the help.

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