Code block refuses to work

I Have a full code block that refuses to comply. this is the code

	function EmitChildren(Attach)
		local Time = 0
		for _, Particle in pairs(Attach:GetChildren()) do
			if Particle:IsA("ParticleEmitter") then
				
				local Count = Particle:GetAttribute("EmitCount") or 0 
				Particle:Emit(Count)
							
			end
		end
	end

-- ...  random extra code

	local Part = Explosion.Explosion
	local E1 = Part:WaitForChild("Part1") -- attachments that house particle emitters
	local E2 = Part:WaitForChild("Part2")
	local EB = Part:WaitForChild("Bottom")
	
	local NewSound = Sounds.Sparkle:Clone()
	NewSound.Parent = Part
	NewSound.PlayOnRemove = true
	NewSound:Destroy()

	EmitChildren(E1) -- emits first particle set
	task.wait(1)
  • the code is to simply just emit all the particles under an attachment along with playing a sound.
  • However NONE of these occur.
  • Placing this code farther infront of the script allow for them to work (play the sound and emit the particles) but I simply cannot do that.
  • The code is for a custom explosion effect. the particles are its own VFX.
  • further down the line the function EmitChildren(Attach) is played on the other two attachments and work perfect.

  • removing the task.wait(1) causes none of the future EmitChildren(Attach) to work aswell

  • I suspect that the issue is that the texture arent loading fast enough but loading them using the content provider jus stops all the code.

you can see the full script below

local ReplicatedStorage = game.ReplicatedStorage
local TNTClient = ReplicatedStorage.Remotes.TNTClient
local Ragdoll = require(game.ServerScriptService.Ragdoll)
local Sounds = ReplicatedStorage.Sounds
local ExplodeSound = Sounds.Explode

local Debris = game:GetService("Debris")
local ContentProvider = game:GetService("ContentProvider")

local TNT = {}

function Bottom(Part, length)
	local rayOrigin = Part.Position
	local rayDirection = Vector3.new(0, -length, 0)

	local raycastParams = RaycastParams.new()
	raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
	raycastParams.FilterDescendantsInstances = {Part}

	local raycastResult = workspace:Raycast(rayOrigin, rayDirection, raycastParams)

	if raycastResult then
		return raycastResult.Position
	else
		return nil
	end
end

function EmitChildren(Attach)
	local Time = 0
	for _, Particle in pairs(Attach:GetChildren()) do
		if Particle:IsA("ParticleEmitter") then
			
			local Count = Particle:GetAttribute("EmitCount") or 0
			Particle:Emit(Count)
						
		end
	end
	
	--Attach:Destroy()
end

function PreLoad(Attach)
	for _, Particle in pairs(Attach:GetChildren()) do
		if Particle:IsA("ParticleEmitter") then
			ContentProvider:PreloadAsync({Particle.Texture}, function()
				warn("something")
			end)
		end
	end
	print("All loaded")
end


function TNT.Spawn(
	Pos: Vector3, 
	Range: number, 
	Damages: NumberArray, 
	Force: number, 
	Burns: BoolValue
)
	
	if not Pos then return warn("no position passed") end
	if Range < 5 then Range = 5 end
	if not Burns then Burns = false end
	if not Force then Force = 30 end
	if not Damages then Damages = {1, Force * 2} end
	
	local Explosion = script.Explosion:Clone()
	Explosion.Parent = workspace
	
	Explosion:ScaleTo(Range/5)
	Explosion.PrimaryPart.Position = Pos
	
	local Part = Explosion.Explosion
	local E1 = Part:WaitForChild("Part1")
	local E2 = Part:WaitForChild("Part2")
	local EB = Part:WaitForChild("Bottom")
	
	local NewSound = Sounds.Sparkle:Clone()
	NewSound.Parent = Part
	NewSound.PlayOnRemove = true
	NewSound:Destroy()

	EmitChildren(E1)
	task.wait(1)

	Explosion.PrimaryPart.Touched:Connect(function(hit)
		if Explosion.PrimaryPart.CanTouch and not hit.Anchored then

			local Difference = Explosion.PrimaryPart.Position - hit.Position
			local UnitVector = Difference.Unit
			local Velocity = UnitVector * Force * 10e2
			local Distance = Difference.Magnitude

			if game.Players:GetPlayerFromCharacter(hit.Parent) then
				local player = game.Players:GetPlayerFromCharacter(hit.Parent)
				local character = player.Character
				local humanoid = character:FindFirstChildOfClass("Humanoid")

				local Damage = math.ceil(math.max(Damages[1], Damages[2] * .5 * Distance/Range))

				ReplicatedStorage.Bindables.ExplosionHit:Fire(character, hit, Damage)

				humanoid:TakeDamage(Damage)

				character.PrimaryPart:ApplyImpulse(Velocity)
			else

				hit:ApplyImpulse(Velocity)
			end

		end
	end)
	
	local NewSound = ExplodeSound:Clone()
	NewSound.Parent = Part
	NewSound:Destroy()
	
	Part.Transparency = 0	
	delay(.1, function()
		Part.Transparency = 1

	end)
	
	task.spawn(function()
		if Bottom(Part, Range/4) then
			EmitChildren(EB)
		end
	end)
	
	EmitChildren(E2)
	task.wait(.5)
	Explosion.PrimaryPart.CanTouch = false

	--game:GetService("Debris"):AddItem(Explosion, 3)
end


return TNT

Can someone please tell me what am I doing wrong? been trying to crack this for 5 hours now.

Consider using breakpoints to see exactly how Luau walks through your code.

When Luau gets to this line, it will start debugging so you can see variable values and manually step to the next lines!

2 Likes

Personally I prefer prints and warns. And all values seem correct or here there supposed to be. Particle and the particle attribute do exist. And if I’m correct I think I said that placing this exact code block further infront causes them to work normally again.

From wt I can guess the sound and the particle emitter don’t load. But that’s still my only guess. I’ll still try breakpoints tomorrow. But if you have other ideas that would be awesome. And also thank you for replying this issue has made me beyond confused

Here is a video of the bomb effect. Thee is one second were the bomb is gone and nothing is happening. That is when E1 is supposed to be playing along with the sparkle sound. I also can’t just forget about E1 and just skip the wait as well.

Removing the way after E1 causes the rest of the particles to also break

The code is waiting 1 second here, is that intentional?

2 Likes

Yes it is. Two reasons

  • so that while the first particle effect that’s supposed to play is uninterrupted by everything else.
  • Second removing it breaks the rest of the particles. As in they don’t play. I’m sure this has something to do with loading but I’m not sure how to implement it

You can i suppose try it out your self here if you wish. The bomb on the 5th slot is the one that explodes.

So the particles and sound work, it’s just delayed. How exactly are you calling TNT.Spawn? Maybe it’s delayed before that is even called.

1 Like

No they don’t work if they are called in tha place. After the wait they work. Before the task.wait(1) they don’t.

Are you making particles in server ? if so then clients have get delayed data about particles (Roblox Network)

solution is sending them data in remote-event and then they have local-script which (assumingly) has logic of making this data into 3D patricles, setting it up with data (received) from server

1 Like

Alright I will try this.

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