While loop and attempt to index nil with 'FindFirstChild' error BUG plz help

local Part = script.Parent

while true do wait()
	local Check = workspace:GetPartsInPart(Part)
	local Active = true

	for i, v in ipairs(Check) do
		if v.Parent then
			local Humanoid = v.Parent:FindFirstChild("Humanoid")
			if v.Name == "HumanoidRootPart" or Humanoid then
				local Player = game.Players:GetPlayerFromCharacter(v.Parent)
				local Damage = Player:WaitForChild("DamageActive")
				if Damage then
					if Damage.Value == true then
						Active = false
						game.Players:GetPlayerFromCharacter(v.Parent).DamageActive.Value = Active
						v.Parent.Humanoid:TakeDamage(25)
						print("Hit")
						wait(1)
						Active = true
						if Damage then
							Damage.Value = Active
						end
					elseif Damage.Value == false then
						wait(1)
						Damage.Value = true
					end
				end
			end
		end
	end
end

Error I’m getting: :roll_eyes: :cold_sweat: :cry:

Workspace.Damage.Script:12: attempt to index nil with 'FindFirstChild'

Usually, when I get errors like this, I fix them by using the .CharacterAdded event. But this error is happening on a while loop and I have no Idea how to fix this problem.

Plz help :point_right: :point_left: :flushed: :people_hugging:

Is your part’s CanCollide false or something? Otherwise, just connect the .Touched event instead.

Yes the CanCollide is false, I’m not using .Touched because it’s inconsistent and worse for R15 avatars

1 Like

Just had a random idea. Would a while loop with a wait(1) that damages the characters humanoid by 25 if the bool is set to true work?

Maybe change this to:

if v.Parent:FindFirstChildOfClass("Humanoid") then

It may have just detected some other part and errored out because it wasn’t actually a Player in the part


Could you elaborate on this?

Above

local Humanoid = v.Parent:FindFirstChild("Humanoid")

Maybe add

if not v.Parent:FindFirstChild("Humanoid") then return end
while true do wait(1)
	script.Parent.RemoteEvent:FireServer()
end
script.Parent.RemoteEvent.OnServerEvent:Connect(function(Player)
	local Active = true
	
	local Part = workspace.DamageTest
	local Check = workspace:GetPartsInPart(Part)
	for i, v in ipairs(Check) do
		if v.Parent then
			if v.Parent.Name == Player.Name then
				print("Same")
				local Humanoid = v.Parent:FindFirstChild("Humanoid")
				if Active then
					Active = false
					Humanoid:TakeDamage(25)
					wait(1)
				end
			end
		end
	end
end)

This is what I meant lol, it works but it’s kinda ugly.

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