Firework hitbox issue

im trying to make a firework dispenser like minecraft but i have a problem with the firework exploding when it touches anything heres the script:

local TweenService = game:GetService("TweenService")
local doing = false

local function wait2()
	task.wait(1)
	doing = false
end

local function poof(firework)
	if firework.Can.Value == true then
		firework.Can.Value = false
		for _, v in pairs(firework.poof:GetDescendants()) do
			if v:IsA("ParticleEmitter") then
				for _, part in pairs(firework:GetDescendants()) do
					if part:IsA("BasePart") and part.Name ~= "poof" then
						part:Destroy()
					end
				end
				v:Emit(50)
				firework.poof.Explosion:Play()

				wait(3)

				firework:Destroy()
			end
		end
	end
end

script.Parent.ClickDetector.MouseClick:Connect(function(player)
	if not doing then
		local stillgoing = true
		doing = true
		task.spawn(wait2)
		script.Parent.Firework:Play()
		local firework = game.ReplicatedStorage.Firework:Clone()
		firework.Parent = workspace
		firework:SetPrimaryPartCFrame(script.Parent.CFrame + Vector3.new(0, 2, 0))

		local goalPosition = firework.PrimaryPart.Position + Vector3.new(0, 50, 0)
		local tweenInfo = TweenInfo.new(3, Enum.EasingStyle.Sine, Enum.EasingDirection.Out)

		for _, v in pairs(firework:GetDescendants()) do
			if v:IsA("BasePart") then
				local goal = {Position = goalPosition}
				local tween = TweenService:Create(v, tweenInfo, goal)
				tween:Play()
			end
		end

		wait(0.1)

		local fireworkParts = firework:GetDescendants()
		local primaryPart = firework.PrimaryPart
		local regionSize = primaryPart.Size + Vector3.new(5, 5, 5)  -- Adding a buffer around the firework's size
		local regionCenter = primaryPart.Position
		local region = Region3.new(regionCenter - regionSize/2, regionCenter + regionSize/2)

		local touched = false

		game:GetService("RunService").Heartbeat:Connect(function()
			local partsInRegion = workspace:FindPartsInRegion3(region, nil, math.huge)
			for _, part in pairs(partsInRegion) do
				if part.Parent and part.Parent ~= script.Parent and not part:IsDescendantOf(firework) then
					poof(firework)
					touched = true
					stillgoing = false
					break
				end
			end
			if touched then
				return
			end
		end)

		wait(3)

		if firework and stillgoing then
			poof(firework)
		end
	end
end)

use FindPartsInRegion3WithIgnoreList insted of FindPartsInRegion3, doth are depreciated but still work. The not depreciated function is GetPartBoundsInBox, but using that you will have to write your own ignorelist function.
Things you want to put in the ignore list are probably terrain, the firework model and the box or whatever the fireworks fire from. You will need to make a list of all the parts and supply that list to the function,.

i tried it but it doesnt work im not good at regions

local TweenService = game:GetService("TweenService")
local doing = false
local ignorelist= {*put list of parts here*}
local function wait2()
	task.wait(1)
	doing = false
end

local function poof(firework)
	if firework.Can.Value == true then
		firework.Can.Value = false
		for _, v in pairs(firework.poof:GetDescendants()) do
			if v:IsA("ParticleEmitter") then
				for _, part in pairs(firework:GetDescendants()) do
					if part:IsA("BasePart") and part.Name ~= "poof" then
						part:Destroy()
					end
				end
				v:Emit(50)
				firework.poof.Explosion:Play()

				wait(3)

				firework:Destroy()
			end
		end
	end
end

script.Parent.ClickDetector.MouseClick:Connect(function(player)
	if not doing then
		local stillgoing = true
		doing = true
		task.spawn(wait2)
		script.Parent.Firework:Play()
		local firework = game.ReplicatedStorage.Firework:Clone()
		firework.Parent = workspace
		firework:SetPrimaryPartCFrame(script.Parent.CFrame + Vector3.new(0, 2, 0))

		local goalPosition = firework.PrimaryPart.Position + Vector3.new(0, 50, 0)
		local tweenInfo = TweenInfo.new(3, Enum.EasingStyle.Sine, Enum.EasingDirection.Out)

		for _, v in pairs(firework:GetDescendants()) do
			if v:IsA("BasePart") then
				local goal = {Position = goalPosition}
				local tween = TweenService:Create(v, tweenInfo, goal)
				tween:Play()
			end
		end

		wait(0.1)

		local fireworkParts = firework:GetDescendants()
		local primaryPart = firework.PrimaryPart
		local regionSize = primaryPart.Size + Vector3.new(5, 5, 5)  -- Adding a buffer around the firework's size
		local regionCenter = primaryPart.Position
		local region = Region3.new(regionCenter - regionSize/2, regionCenter + regionSize/2)

		local touched = false

		game:GetService("RunService").Heartbeat:Connect(function()
			local partsInRegion = workspace:FindPartsInRegion3WithIgnoreList(region, ignorelist, math.huge)
			for _, part in pairs(partsInRegion) do
				if part.Parent and part.Parent ~= script.Parent and not part:IsDescendantOf(firework) then
					poof(firework)
					touched = true
					stillgoing = false
					break
				end
			end
			if touched then
				return
			end
		end)

		wait(3)

		if firework and stillgoing then
			poof(firework)
		end
	end
end)

ye thx i modified it and it worked

1 Like