its supposed to hit a part
if hit and not hit.Parent:FindFirstChild(“Humanoid”) and hit.CanCollide == true then
it says “attempting to index nil with :FindFirstChild()”
please provide the code so people can help you better.
Touched is used on parts or similar to see if some other part/humanoid or anything touched it.
for example:
Part.Touched:connect(function(a)
print(a:GetFullName())
end)
i know and it says that the hit.Parent is nil even though it sometimes works and sometimes doesnt
Keep in mind next time to use triple back ticks ``` to format your post.
So for your problem:
This means there is something wrong with:
--This as this becomes nil
hit.Parent
As you said. Try figuring it out by doing some print debugging and see what happens.
print(hit)
print(hit.Parent)
i already tried it and it doesnt work
How does it not work? what do you notice happens? Does anything occur? an error in output?
We will require more information if you want us to help with your problem.
i literally told you everything that you needed to know
And the error message already told you the error too lol. Plus you haven’t sent the script in the first place, there might be more errors than you can notice.
yes what im saying here is what is making it not work on this but on other scripts it works
i used the same code here in other scripts
Please provide your full code because it is really difficult to know what is wrong with the short piece you provided. Also what are you trying to achieve?
So far all I can gather is that your code looks something like this:
local Workspace = game:GetService("Workspace")
local Part = Workspace.Part
Part.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") and hit.CanCollide == true then -- Shouldn't CanCollide be false?
-- Do something
print("Hitting part")
end
end)
The code above works as indented with no errors. Why are you checking the hit parts CanCollide property? I am running the script in ServerScriptService in a server script.
It probably means that the hit has no parent so check it like this.
function onTouch(hit)
if hit.Parent ~= nil then
--your code here
end
end
did you read what i said? im trying to make it hit a part and when it hits the part it says the error that i said it says.
its a touched event theres no need to tell you the code its literally just touched and the error isnt from the code after its getting touched its only when its getting touched
i already tried it it doesnt work
Hmm, did you read what we said?
Can’t help you with your code and it’s errors if you don’t give us
-
Where the script is
-
What is in the script
Did you connect the function to the Touched event? plus whats the error when you tried it?
its a tool inside starterpack
there is a localscript that fires a remoteevent when you hold x and let go of x and then theres a part and i want that part whenever he touches another part to make explosions and i know how to do it its just that it doesnt work
i posted the error scroll up and you will find it
Im talking about the error when you tried this.
function onTouch(hit)
if hit.Parent ~= nil then
--your code here
end
end
i already tried doing if hit.Parent ~= nil all it does is removing the error and the touched event still doesnt work
@OctaLua has helped you solved the indexing problem which is the error so this has worked which is what you asked for in the beginning:
Meanwhile, we have no idea what you actually want to do with the touched event.
Sorry, but we will not respond anymore unless you provide sufficient information such as the full script pertaining the problem.