Destroy(Function) Better way

Hello all, this is my first made up script, testing variables, wait timers and destroy() function. What’s the best way of achieving this without all that code? Not entirely sure a table would work here. I was thinking maybe a repeat, or tween service?

I plan on turning the block into a TNT mesh.

local SelfDestructTimer = 0.2 --Edit me... / Choose how long it takes for a part to be destroyed after touch
local BCTimer = 0.5 -- Edit me... / Controls the brick color change

--6.2 second timer
----------------------------------------------------------------------------------------------------------
script.Parent.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid")then
		script.Parent.BrickColor = BrickColor.new("Red flip/flop")
		wait(BCTimer)
		script.Parent.BrickColor = BrickColor.new("medium Medium stone grey")
		wait(BCTimer)
		script.Parent.BrickColor = BrickColor.new("Red flip/flop")
		wait(BCTimer)
		script.Parent.BrickColor = BrickColor.new("medium Medium stone grey")
		wait(BCTimer)
		script.Parent.BrickColor = BrickColor.new("Red flip/flop")
		wait(BCTimer)
		script.Parent.BrickColor = BrickColor.new("medium Medium stone grey")
		wait(BCTimer)
		script.Parent.BrickColor = BrickColor.new("Red flip/flop")
		wait(BCTimer)
		script.Parent.BrickColor = BrickColor.new("medium Medium stone grey")
		wait(BCTimer)
		script.Parent.BrickColor = BrickColor.new("Red flip/flop")
		wait(BCTimer)
		script.Parent.BrickColor = BrickColor.new("medium Medium stone grey")
		wait(BCTimer)
		script.Parent.BrickColor = BrickColor.new("Red flip/flop")
		wait(BCTimer)
		script.Parent.BrickColor = BrickColor.new("medium Medium stone grey")
		wait(SelfDestructTimer)
		script.Parent:Destroy()
	end
end)

1 Like

Use a for loop:

--//Variables
local Part = script.Parent

--//Controls
local SelfDestructTimer = 0.2 --Edit me... / Choose how long it takes for a part to be destroyed after touch
local BCTimer = 0.5 -- Edit me... / Controls the brick color change
local Debounce = false

--//Functions
Part.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") and not Debounce then
        Debounce = true

		for i = 1, 10 do
			Part.BrickColor = BrickColor.new("Red flip/flop")
			task.wait(BCTimer)
			
			Part.BrickColor = BrickColor.new("Medium stone grey")
			task.wait(BCTimer)
		end

		task.wait(SelfDestructTimer)
        Debounce = false
		Part:Destroy()
	end
end)
2 Likes

You need to loop this.

for i = 1, 10 do
	script.Parent.BrickColor = BrickColor.new("Red flip/flop")
	wait(BCTimer)
	script.Parent.BrickColor = BrickColor.new("medium Medium stone grey")
	wait(BCTimer)
end

Replace the code that changes the colors with this.

Edit: @Katrist beat me to it, gg

2 Likes
local SelfDestructTimer = 0.2 --Edit me... / Choose how long it takes for a part to be destroyed after touch
local BCTimer = 0.5 -- Edit me... / Controls the brick color change
local Iterations = 6
local Debounce = false

--6.2 second timer
----------------------------------------------------------------------------------------------------------
script.Parent.Touched:Connect(function(hit)
    if not Debounce and hit.Parent:FindFirstChild("Humanoid") then
        Debounce = true
        
        for i = 0, Iterations do
            script.Parent.BrickColor = BrickColor.new("Red flip/flop")
            task.wait(BCTimer)
            script.Parent.BrickColor = BrickColor.new("Medium stone grey")
            task.wait(BCTimer)
        end
        task.wait(SelfDestructTimer)
        script.Parent:Destroy()
    end
end)

1 Like

Can I just confirm my understanding is correct… I was under the impression that tables were meant to be for i, v in pairs. Which is why I didn’t think tables would work.

This piece of the coding “for i = 1, 10 do” Is it understood as “for i = 1” which is stating that i is == to 1 and the 10 is how many times it’ll play?

Numerical loops go like this:

index = start, stop, step

index is the current iteration the loop is on
start is where the numerical loop will begin iteration
stop is where the numerical loop will stop iteration
step is the increment (or decrement) when iterating (default 1)

For example:

for i = 1, 10, 2 do
   print(i) -- This will print every 2 digits starting from 1
end

So yes, 10 is how many times it’ll play

2 Likes

10 is how much it plays. It just counts from 1 to 10. @HugeCoolboy2007 has a really good explanation.

1 Like