Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How Do I make a countdown inside a script and then make an object explode?

I am making a game where you have to click bombs before they explode and I can't make a timer that when it hits 30 the bomb will explode. THE PARTS WITH ASTRICTS ARE THE PARTS I NEED HELP WITH, BUT FEEL FREE TO LOOK AT THE REST IF THERE IS AN ISSUE :)

local clidet = script.Parent.ClickDetector -- finds the clickdetector
local prtdestroy = script.Parent -- finds the bomb
local prtmaxhealth = 500 -- bombs max health
local prthealth = prtmaxhealth -- sets the beginning health to the max
local prtdeath = 0 -- bombs health that it dies at
local weapondmg = 10
**local boomseconds = 30**

clidet.MouseClick:Connect(function() -- checks when the click detector is clicked
	if prtdestroy.Parent == game.Workspace then -- checks if the parent of the bomb is workspace
		wait(0.2) -- delay before next hit
		prthealth = prthealth - weapondmg -- subtracts health
		if prthealth == prtdeath then -- checks if the health of the bomb is the health that it dies at
			prtdestroy:Destroy() -- destroys the part
		end
	end
end)

**for i = 1,boomseconds.Value do
 wait(1)
 boomseconds.Value = boomseconds.Value - 1
if boomseconds == 0 then
	local explosion = Instance.new("Explosion")
	explosion.Parent = prtdestroy
	prtdestroy:Destroy()
end
end**

1 comment

0

maybe doing the line 22 local explosion = Instance.new("Explosion",workspace)

4 answers

You actually have it correct for the most part, but there are two things that you are doing wrong, first of all, I would use a while loop instead of a for loop, I don't know why you decided to use a for loop for the timer, it's functional, but I don't suggest it. Second thing is that you have the loop outside the clickdetector check when you check for the bomb's "health" to be equal to 0. The loop should be under that part where the bomb health = 0.

It should look something like this:

local clidet = script.Parent.ClickDetector -- finds the clickdetector
local prtdestroy = script.Parent -- finds the bomb
local prtmaxhealth = 500 -- bombs max health
local prthealth = prtmaxhealth -- sets the beginning health to the max
local prtdeath = 0 -- bombs health that it dies at
local weapondmg = 10
local boomseconds = 30

local debounce = false -- add a debounce so they can't spam click it
clidet.MouseClick:Connect(function() -- checks when the click detector is clicked
    if prtdestroy.Parent == game.Workspace and not debounce then 
   		debounce = true
       	wait(0.2) -- delay before next hit
    	debounce = false
       	prthealth = prthealth - weapondmg -- subtracts health
       	if prthealth == prtdeath then 
            while (boomseconds > 0) do 
                wait(1)
	        boomseconds = boomseconds - 1 
	    end
	    local explosion = Instance.new("Explosion", prtdestroy) -- explode after the timer
	    prtdestroy:Destroy()
	end
    end
end)

My apologies, the indentations are ending up weird on the code part that I provided, even though when I type it, it shows that it is correct.

Hope I helped, let me know if you have any questions

You made things very complicated. You can simply do:

local clidet = script.Parent.ClickDetector -- finds the clickdetector
local prtdestroy = script.Parent -- finds the bomb
local prtmaxhealth = 500 -- bombs max health
local prthealth = prtmaxhealth -- sets the beginning health to the max
local prtdeath = 0 -- bombs health that it dies at
local weapondmg = 10
local boomseconds = 30

clidet.MouseClick:Connect(function() -- checks when the click detector is clicked
    if prtdestroy.Parent == game.Workspace then -- checks if the parent of the bomb is workspace
        wait(0.2) -- delay before next hit
        prthealth = prthealth - weapondmg -- subtracts health
        if prthealth == prtdeath then -- checks if the health of the bomb is the health that it dies at
            prtdestroy:Destroy() -- destroys the part
        end
    end
end)

wait(boomseconds)
 local explosion = Instance.new("Explosion")
explosion.Parent = prtdestroy
prtdestroy:Destroy()

You added an unneeded for loops.

4 comments

0

I don't think you understand. He wants to broadcast each second to people who see it instead of just waiting. Like this: 30, 29, 28. Everyone can see each change

0

That's why he used the loops

0

But idk

0

In that case, he was still treating boomseconds as a numbervalue/intvalue when he defined it as a number at the top of the script.

while true do
	wait(1)
	boomseconds.Value = boomseconds.Value - 1
	if boomseconds.Value == 0 then
		local explosion = Instance.new("Explosion")
    		explosion.Parent = prtdestroy
    		prtdestroy:Destroy()
		break
	end
end

You would want to start at the time you want to be at, then set a goal of time that you want to achieve, and then subtract by how much you want it to be at for when you reach your goal.

for = boomseconds, 1, -1 do --// Start at boomseconds (30), go to 1, by subtracting -1, and waiting 1 second in between.
	wait(1)
	if boomseconds == 0 then
		local explosion = Instance.new("Explosion")
		explosion.Parent = prtdestroy
		prtdestroy:Destroy()
	end
end

This will give you a count down starting at boomseconds, which is 30. And it will go down to your goal, which is 1, and it will subtract boomseconds by -1 to reach 1.

For Loops

Hope this helped!

This discussion is preserved as it was. New answers, comments, and votes are closed.