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