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)
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.
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.
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.
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)