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)
--//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)
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.
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)
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?
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