Attempt to index nil with 'Parent' Error = (FindFirstChildOfClass)

Hello, good morning, afternoon and evening. I have a problem and I don’t know how to solve it. Can someone solve it and give me feedback? I am a newbie who is learning to program

image

local modelo = script.Parent
local placa = modelo.Placa
local Bola = modelo.Bola
local puerta = modelo.Puerta

local debounce = true
local nuevaBola = nil

local debris = game:GetService("Debris")

function tocar(hit)
------------Error ↷  ----------
	local hum = hit.Parent:FindFirstChildOfClass("Humanoid")  --  🡨 Error 
	if not hum then return end
	if debounce then return end
	debounce = false
	puerta.CanCollide = false
	puerta.Transparency = 1



	debris:AddItem(Bola, 8)
end

function trampa()
	nuevaBola = Bola:Clone()
	nuevaBola.Anchored = false
	nuevaBola.Transparency = 0
	nuevaBola.Parent = modelo
	nuevaBola.CanCollide = true

	puerta.Transparency = 0
	puerta.CanCollide = true

	nuevaBola.Destroying:Connect(trampa)

	debounce = true
end


placa.Touched:Connect(function(hit)
	tocar()

end)
trampa()

image
It seems like you have forgotten to add “hit” into tocar(). Should be like this: tocar(hit)

Oh and by the way, in this line here there could be a problem later on:

local hum = hit.Parent:FindFirstChildOfClass("Humanoid")

If for example your leg is the “hit” here everything is fine. But if a part inside an accessory gets hit then it wont work because the parent wont be the humanoid.

image

I’d better shorten it but it doesn’t work

Is the error message still the same or is it a diffrent one now?

1 Like

It no longer prints the error, but the touched event still doesn’t work. ;(

try replacing this line:

local hum = hit.Parent:FindFirstChildOfClass("Humanoid")

with this one:

local hum = hit.Parent:FindFirstChildOfClass("Humanoid") or hit.Parent.Parent:FindFirstChildOfClass("Humanoid")

Should fix one of the problems. The post below should fix your entire problem with your script.

2 Likes

If you’ve already fixed the connection using placa.Touched:Connect(tocar), then the issue is how the debounce is set up.

Right now, you’re setting debounce = true at the start of the script, which causes the function tocar to immediately return when triggered because it’s checking if debounce is already true.

To fix this, you should define debounce = false at the start instead, so the first touch can actually go through.

Better Solution If Debounce Has To Be True When Defined
Also, in the trampa function, you’re setting debounce = true again, so instead of changing the debounce at the start of the script, you can just change it inside of the trampa function.

1 Like

There are more errors, it works but the ball is not cloned again, also it is not destroyed with the debris service

nuevaBola = Bola:Clone()
debris:AddItem(Bola, 8)

Another mistake I made was misusing the early return, missing “not”

if not debounce then return end
if not debounce then return end
	debounce = false
debounce = true

I had made a mistake with early return, I forgot to put not in dobunce, but now the new problem is that I can’t clone Part:Clone

local modelo = script.Parent
local placa = modelo.Placa
local Bola = modelo.Bola
local puerta = modelo.Puerta

local debounce = true
local nuevaBola = nil

local debris = game:GetService("Debris")

function OnTouch(hit)

	local hum = hit.Parent:FindFirstChildOfClass("Humanoid")
	print("1")
	if not hum then return end
	if not debounce then return end
	debounce = false
	
	puerta.CanCollide = false
	puerta.Transparency = 1



	debris:AddItem(Bola, 5)
	print("2")
end

function trampa()
	print("clone")
	nuevaBola = Bola:Clone()
	nuevaBola.Anchored = false
	nuevaBola.Transparency = 0
	nuevaBola.Parent = modelo
	nuevaBola.CanCollide = true

	puerta.Transparency = 0
	puerta.CanCollide = true

	nuevaBola.Destroying:Connect(trampa)

	debounce = true
	print("3")
end


placa.Touched:Connect(OnTouch)
trampa()

I think what you meant to do was add the nuevaBola to the debris instead of the original Bola?

If this doesn’t fix the problem, then check for visual errors and send them.

1 Like

I made several mistakes but I already found them, first
1.
I was erasing the original rock when I had to erase Nill

local Bola = modelo.Bola --Original Rock
local nuevaBola = nil  --NewRock

debris:AddItem(Bola, 8)

Write the variable just like the value (Bola = Bola)

Bola = modelo.Bola

I was creating an anonymous function that calls Ontouch () without arguments
Correct options

function tocar(hit)
	
end

placa.Touched:Connect(Ontouch)
---------Or

placa.Touched:Connect(function()
	Ontouch(hit)
end)

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