Touched Event Returning Nil

I am making a Clash Royale-esque game and everything is working fine except for one of the swords one of the characters uses. I have a relatively simple script set up for damaging the player it touches, and it used to work up until just recently. I have not changed anything about the script, but I have added a new character. However this happens with everything and not only the new character.

 local debounce = false
local weapon = script.Parent
local dmg = 5

weapon.Touched:Connect(function(part)
	wait(0.1)
	if part:FindFirstAncestorWhichIsA("Model"):FindFirstChildWhichIsA("Humanoid") and (not part.Parent:FindFirstAncestorWhichIsA("Folder")) ==  script.Parent:FindFirstAncestorWhichIsA("Folder") then
		if debounce == false then
		local humanoid = part.Parent:findFirstChild("Humanoid")
			humanoid:TakeDamage(dmg)
		debounce = true
		wait(0.8)
		debounce = false
				else
					
	end
			end
end)

1 Like

Events don’t return anything. Do you mean that the ‘part’ being passed to your function is nil? Also can you fix the formatting of your code in the post?

Yes, I meant the parameter being passed to my function. Sorry I have not scripted consistently for a while so I forget some terminology. I will fix the formatting.

If you wait(0.1) where you do, it is entirely possible that part can become nil in that time. I would remove this. Any wait in a script is the same as saying “Anything may happen in between here” the other wait before unsetting debounce is fine although there is a preferred other way to write this.

The wait(0.1) was added after I the issue began. I thought that the issue may have been due to the parameter not being properly registered in an instant, but as you can see it did not fix anything.

What is the actual error or thing that is not working?

22:26:53.806 Workspace.RedTowers.Noob.Weapon.Handle.Part.Script:7: attempt to index nil with ‘FindFirstChildWhichIsA’ - Server - Script:7

Your call to :FindFirstAncestorWhichIsA() is returning nil, then you are trying to call FindFirstChildWhichIsA(). Only Instances have this member function, nil does not (its ‘index’ does not contain any functions). This is why you get an error. You need to check each thing step by step instead.

1 Like

Incorrect, look closely at the error. It says that I am attempting to index nil, the parameter “part”, with the method FindFirstChildWhichIsA().

Can you tell me what you think the possible results of this are?:

local result = part:FindFirstAncestorWhichIsA("Model")

What are the possible values of result?

local debounce = false
local weapon = script.Parent
local dmg = 5

weapon.Touched:Connect(function(part)
	wait(0.1)
	local model = part:FindFirstAncestorWhichIsA("Model")
	if model and model:FindFirstChildWhichIsA("Humanoid") and (not part.Parent:FindFirstAncestorWhichIsA("Folder")) ==  script.Parent:FindFirstAncestorWhichIsA("Folder") then
		if debounce == false then
		local humanoid = part.Parent:findFirstChild("Humanoid")
			humanoid:TakeDamage(dmg)
		debounce = true
		wait(0.8)
		debounce = false
				else
				end	
	end
end)

things that touched your “weapon” isn’t always a character.

nil, character model, or workspace. my argument was not against that. my argument was saying that the error is formatted in a way such that it couldnt have been the findfirsthchildwhichisa returning nil.

It stopped giving me errors but its still not doing anything.

ok so let me make it simple

local debounce = false
local weapon = script.Parent
local dmg = 5

weapon.Touched:Connect(function(part)
	wait(0.1)
	local model = part.Parent
	if model and model:FindFirstChildWhichIsA("Humanoid") and not (part.Parent:FindFirstAncestorWhichIsA("Folder") ==  script.Parent:FindFirstAncestorWhichIsA("Folder")) then
		if not debounce then
			local humanoid = part.Parent:FindFirstChild("Humanoid")
			humanoid:TakeDamage(dmg)
			debounce = true
			wait(0.8)
			debounce = false
		else
		end	
	end
end)

You didn’t change anything about the script?

look closely. i changed it :exploding_head:


only difference i see is some capitalization and indentation changes

i never changed the capitalization :thinking:

Oh. My. God. Thank you so much. Those parentheses you added literally fixed everything and it works perfectly now! Sorry for not noticing.

1 Like

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