Why can't I disable the Particle Emitter like this?

Script:

local tween = game:GetService("TweenService")
local invisible = {}
local debounce = true

local function weldFunction(a, b)
	local weld = Instance.new("WeldConstraint",a)
	weld.Part0 = a
	weld.Part1 = b
	
	return weld
end

game.ReplicatedStorage.Smoke.OnServerEvent:Connect(function(player)
	local char = player.Character
	if debounce == true then
		invisible[player] = true

		local smokeBomb = game.ReplicatedStorage.SmokeBomb:Clone()
		smokeBomb.CFrame = char.RightHand.CFrame
		smokeBomb.Parent = workspace
		local weld = weldFunction(char.RightHand, smokeBomb)

		local throwAnim = char.Humanoid:LoadAnimation(script.Throw)
		throwAnim:Play()
		char.HumanoidRootPart.Anchored = true
		debounce = false
		wait(0.8)
		weld:Destroy()
		
		local sound = script.Sound:Clone()
		sound.Parent = smokeBomb
		sound:Play()

		wait(0.25)
		smokeBomb.Smoke.Enabled = true
		if invisible[player] == true then
			for i,v in pairs(char:GetDescendants()) do
				if (v:IsA("BasePart") or v.Name == "face") and v.Name ~= "HumanoidRootPart"  then
					char:FindFirstChildOfClass("Accessory"):FindFirstChildOfClass("BasePart"):FindFirstChildOfClass("ParticleEmitter").Enabled = false
					local tween1 : Tween = tween:Create(v,TweenInfo.new(3), {Transparency = 1})
					tween1:Play()

					tween1.Completed:Connect(function()
						char.HumanoidRootPart.Anchored = false
						task.wait(15)
						local tween2 : Tween = tween:Create(v,TweenInfo.new(3),{Transparency = 0})
						
						tween2:Play()
						smokeBomb:Destroy()

						task.wait(15)
						debounce = true
					end)
				end	
			end
		end
	end
end)

Error:

ServerScriptService.Smoke:39: attempt to index nil with 'FindFirstChildOfClass'  -  Server - Smoke:39
8 Likes

I believe this line of code is the error

char:FindFirstChildOfClass("Accessory"):FindFirstChildOfClass("BasePart"):FindFirstChildOfClass("ParticleEmitter").Enabled = false

This error basically means theres no instance of an Accessory in the players character.

6 Likes

how to check if there is, because i think this only works if there is,right?

5 Likes

You can look in the explorer via studio, just expand everything and see if you can find ParticleEmitter.

5 Likes

This is a bad way to write code, leading to exactly the problem you have now: you have consolidated your indexing onto 1 line, meaning you can’t tell where the error is actually happening.
Keep things clean!

local accessory = char:FindFirstChildOfClass("Accessory")
if accessory then 
  local bp = accessory:FindFirstChildOfClass("BasePart")
    if bp then
      local pe = accessory:FindFirstChildOfClass("ParticleEmitter")
    end
  end
end
5 Likes

Yeah i was thinking that too, I know that you code like that I will try this now!

4 Likes

Now this is my code but it still doesn’t disable the particleEmitter in my Mm2 crown why is that?

local tween = game:GetService("TweenService")
local invisible = {}
local debounce = true

local function weldFunction(a, b)
	local weld = Instance.new("WeldConstraint",a)
	weld.Part0 = a
	weld.Part1 = b
	
	return weld
end

game.ReplicatedStorage.Smoke.OnServerEvent:Connect(function(player)
	local char = player.Character
	if debounce == true then
		invisible[player] = true

		local smokeBomb = game.ReplicatedStorage.SmokeBomb:Clone()
		smokeBomb.CFrame = char.RightHand.CFrame
		smokeBomb.Parent = workspace
		local weld = weldFunction(char.RightHand, smokeBomb)

		local throwAnim = char.Humanoid:LoadAnimation(script.Throw)
		throwAnim:Play()
		char.HumanoidRootPart.Anchored = true
		debounce = false
		wait(0.8)
		weld:Destroy()
		
		local sound = script.Sound:Clone()
		sound.Parent = smokeBomb
		sound:Play()

		wait(0.25)
		smokeBomb.Smoke.Enabled = true
		if invisible[player] == true then
			for i,v in pairs(char:GetDescendants()) do
				if (v:IsA("BasePart") or v.Name == "face") and v.Name ~= "HumanoidRootPart"  then
					local tween1 : Tween = tween:Create(v,TweenInfo.new(3), {Transparency = 1})
					tween1:Play()
					local accessory = char:FindFirstChildOfClass("Accessory")
					if accessory then 
						local bp = accessory:FindFirstChildOfClass("BasePart")
						if bp then
							local pe = accessory:FindFirstChildOfClass("ParticleEmitter")
							pe.Enabled = false
						end
					end
					tween1.Completed:Connect(function()
						char.HumanoidRootPart.Anchored = false
						task.wait(15)
						local tween2 : Tween = tween:Create(v,TweenInfo.new(3),{Transparency = 0})
						
						tween2:Play()
						if accessory then 
							local bp = accessory:FindFirstChildOfClass("BasePart")
							if bp then
								local pe = accessory:FindFirstChildOfClass("ParticleEmitter")
								pe.Enabled = true
							end
						end
						smokeBomb:Destroy()

						task.wait(30)
						debounce = true
					end)
				end	
			end
		end
	end
end)
3 Likes

The exact problem you’re running into is that if any of those terms return nil, because you daisychained them the next one will do nil:findFirstCildOfClass(). Can’t index nil, nil is just nil.

2 Likes

You’re doing,

  1. Is the player invisible? If so, continue
  2. Iterate over all the children of the character.
  3. Is the currently indexed child a BasePart or with the name face? and it isn’t the HRP?
  4. If yes, then find the first accessory.
  5. If an accessory has been found, find a BasePart child.
  6. If a BasePart child is found, find a particle emitter child.
    7, Disable it.

If you have 2 accessories, this will break, because it will keep only interacting with the 1st found accessory, every iteration.

2 Likes

Yeah and I my character has 2 accessories so how to do that?

1 Like

Well, you should just take the whole accessory thing out of the if function.



if invisible[player] == true then
			for i,v in pairs(char:GetDescendants()) do
                if v:IsA("Accessory") then
                -- Just turn it off here
                end
				if (v:IsA("BasePart") or v.Name == "face") and v.Name ~= "HumanoidRootPart"  then
					local tween1 : Tween = tween:Create(v,TweenInfo.new(3), {Transparency = 1})
					tween1:Play()
					local accessory = char:FindFirstChildOfClass("Accessory")
					
					tween1.Completed:Connect(function()
						char.HumanoidRootPart.Anchored = false
						task.wait(15)
						local tween2 : Tween = tween:Create(v,TweenInfo.new(3),{Transparency = 0})
						
						tween2:Play()
						if accessory then 
							local bp = accessory:FindFirstChildOfClass("BasePart")
							if bp then
								local pe = accessory:FindFirstChildOfClass("ParticleEmitter")
								pe.Enabled = true
							end
						end
						smokeBomb:Destroy()

						task.wait(30)
						debounce = true
					end)
				end	
			end
		end

You already iterate over all the children of char, may aswell directly interact with accessories instead of through the other parts

3 Likes

Perhaps you could try this and read the output:

if invisible[player] == true then -- this is always true btw, cause you did set that player to true since the start of the event

	local pe = char:FindFirstChildWhichIsA("ParticleEmitter", true) -- check if any particle inside player exist
	if pe then
		pe.Enabled = false
	else
		warn("no particle inside character")
	end

	for i,v in pairs(char:GetDescendants()) do
		if (v:IsA("BasePart") or v.Name == "face") and v.Name ~= "HumanoidRootPart"  then
			local tween1 : Tween = tween:Create(v,TweenInfo.new(3), {Transparency = 1})
			tween1:Play()

			tween1.Completed:Connect(function()
				char.HumanoidRootPart.Anchored = false
				task.wait(15)
				local tween2 : Tween = tween:Create(v,TweenInfo.new(3),{Transparency = 0})

				tween2:Play()
				if pe then 
					pe.Enabled = true
				end
				smokeBomb:Destroy()

				task.wait(30)
				debounce = true
			end)
		end	
	end

end
5 Likes

Yeah I know that this is always true, i just put in because I did change the script but forget to put it out!

3 Likes

Thank you very much this worked for me, thanks for the help :smiley:

4 Likes

Awesome, just be aware that this:
local pe = char:FindFirstChildWhichIsA("ParticleEmitter", true)

Is looking for ANY particle inside the character, if there is any other particle which is not the one you are looking for, it will work on that particle. So be sure to add more checks or change the approach

3 Likes

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