How to dectect when a explosion has finished

Main Issue:
Our goal here is to get a instant amount of all the parts that were exploded. We have added +1 to the amount each time explosion.hit was fired. It prints the amount inside of the event immediately but outside the event it prints 0 each time.

The code:

local amount = 0
explosion.Hit:Connect(function(part)	
		--LEAVE----------------------------------------------------
		if part.Parent == nil then return end
		-----------------------------------------------------------
		if not CanFire(player, part.Parent.Parent.Parent) then return end
		
		--BASIC CHECKS---------------------------------------------
		if not part:IsA("BasePart") then return end
		-----------------------------------------------------------
		
		local numberValue = part.Parent.Parent.Parent.Configuration.RemovedParts
		
		amount += 1
		numberValue.Value += 1
	end)
print(amount)

Please let us know if there are any solutions to this! Thank you.

I might be mistaken, but the issue is that this prints once the script executes. It wouldn’t wait for the code inside the event to execute.

Okay, do you have an idea on how we would wait for the event to fire?

I am pretty sure you should put the print statement inside the event (at the end)

Edit: Also you should probably put the very first line in the script as the first line inside the event, so that it resets each time (assuming you only want the amount of parts for that specific time)

by putting the print on the first line inside the event

I’m not too sure what you mean by this?

We’ve tired this the problem is we want to be able to get the entire value of all the parts that were hit by an explosion. This just gives us each individual part.

Oh sorry, well you could add a check by adding this at the top of the script:
local event = false

And then put this as the last line in the event:
event = true

And replace the print statement with this:
while task.wait() do
if event == true then
print(amount)
end
end

The problem with this is we need a total amount of exploded parts. All this would do is once one part has exploded the event would be true.

local amount = 0
local explosion = Instance.new("Explosion")
explosion.Parent = game.Workspace.SpawnLocation

for _, plr in game.Players:GetPlayers() do
	local dist = (explosion.Position - plr.Character.PrimaryPart.Position).Magnitude
	if dist >= explosion.BlastRadius then
		task.wait(5) -- wait for explosion to be done? idk if there's a better method
		for _, bodypart in plr.Character:GetChildren() do
			if bodypart:IsA("BasePart") then
				amount += 1
			end
		end
	end
end
print(amount.. "Body Limbs left!")

This would only work for one person, though if you want it to work for multiple, either use tables or make the variable inside the loop.

Sorry but I am on mobile right now, therefore I am unable to test so I can’t really help, and also I don’t fully understand when you want it to print.

We figured out a solution, thank you for you’re responses and help! All we added was a task.wait() before printing the final amount.:+1:

A better way you can do is something like this?

repeat task.wait() until explosion.Parent == nil
task.wait()

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