Why does :Emit() not work?

so am remastering that cube script and trying to make aparticle emit but gives me a error?

-- this is a script in serverscriptservice
local Cubes = game.Workspace.CubesFolder
local DB = false

for _, Cube in pairs(Cubes:GetChildren()) do
	Cube.Touched:Connect(function(hit)
		local char = hit.Parent
		local hum = char:FindFirstChild("Humanoid")
		local hrp = char:FindFirstChild("HumanoidRootPart")
		if hum and hum.Parent == char and not DB then
			local particle = 	hrp:FindFirstChild("HitParticle")
			DB = true
			local player = game.Players:GetPlayerFromCharacter(char)
			player.leaderstats.Cubes.Value += 1
			particle:Emit(1)
			Cube:Destroy()
		end
	end)
end
  1. Why do you have to check if hum.Parent == char when you are using :FindFirstChild()?
  2. You never reset DB to false. (I don’t know if this is intentional)
1 Like

Would be nice to know the error it gave you.

1 Like

alr
ServerScriptService.CubeHandlerScript:15: attempt to index nil with ‘Emit’

A couple of suggestions:

  • If this is for 1 individual Cube, that’s fine but if you’re using multiple why not encase the DB in with the loop?

  • Your particle variable could be defined as nil if there isn’t even a HitParticle in there to begin with

  • It could be possible that it is calling Emit(), but you’re only just emitting 1 particle & that’s it

  • Also check for the hrp as well, you never know what may happen :man_shrugging:

local Cubes = workspace.CubesFolder
local DB = false

for _, Cube in pairs(Cubes:GetChildren()) do
    Cube.Touched:Connect(function(Hit)
        local Hum = Hit.Parent:FindFirstChild("Humanoid") 
        local HRP = Hit.Parent:FindFirstChild("HumanoidRootPart")
        local Plr = game.Players:GetPlayerFromCharacter(Hit.Parent)

        if not DB and Hum and HRP and HRP:FindFirstChild("HitParticle") and Plr then
            DB = true

            Plr.leaderstats.Cubes.Value += 1
            HRP.HitParticle:Emit(1)
            --wait(Duration)
            --DB = false - Not sure if you need this or not
        end
    end)
end
1 Like

just tested it but gave no result no nothing

This is the problem with your code. The hrp doesn’t have a child named “HitParticle”. Double check the name, that you’re looking for “HitParticle” in hrp, and that you create the particle emitter.