Error in output, but Script works fine

Hi, here is the script, it keeps giving me an error but the script works.
Workspace.Mushroom.Script:6: attempt to index nil with ‘Mushrooms’

local touchRamen = script.Parent.grabMushroom
local db = false

local function grabRamen(hit)
	if hit.Parent.Name == "Ramen" then
			local ramenParts = hit.Parent:FindFirstChild("Handle").Mushrooms
			ramenParts.Transparency = 0
			for i, RamenParts in pairs(ramenParts:GetChildren()) do
			RamenParts.Transparency  = 0
			hit.Parent.Name = "Ramen with Mushrooms" 
			wait(1)
			db = true
			wait(10)
			db = false
				end
			end
		end
	touchRamen.Touched:Connect(grabRamen)

You know, it would be a lot more helpful if you actually told us what the error is

2 Likes

Workspace.Mushroom.Script:6: attempt to index nil with ‘Mushrooms’

:FindFirstChild is basically just a dot operator except it won’t error itself when you’re indexing for an unknown object. The object Handle is not guaranteed and the function might return nil instead, which is what’s happening here. Consider using :WaitForChild instead or put the function onto a separate line to have more control over it.

Also I highly recommend you to read this thread:

I think what is happening is that you could be hitting something which Mushrooms isn’t a value of. So for example you could check if the child exists:

if hit.Parent.Handle.Mushrooms then

You could use this over :FindFirstChild and :WaitForChild as it would be quicker because you arent waiting for something.

DO NOT directly index for the objects. The if statement itself might error and the script will still break

I used :WaitForChild(" ") and now its giving me an Infinite Yield error.

Oh yes, sorry I just realized that doesn’t work, thanks for pointing it out

Then it’s because the object literally doesn’t exist or maybe there’s a spelling error? Check the explorer to see for yourself if the object is really there

Do some hit.Parents 's not have a handle, or do all have a handle?

There is no variable for “Mushrooms” therefore, it’s indexing with nil and is unable to find the child of handle.

It has a handle for the bowl tool