The script I've written is supposed to check if the part "mine" exists, and if it doesn't, create a new one. The problem is that for some reason, when the new mine is created, it will infinitely explode, even when a player is not touching it. Why is this?
part = game.Workspace.mine
while true do
if game.Workspace:FindFirstChild("mine") then
print("the mine is okay") else
function createmine()
local mine = Instance.new("Part")
mine.Name = "mine"
mine.Parent = workspace
mine.Position = game.Workspace:FindFirstChild("master").Position
mine.Size = game.Workspace:FindFirstChild("master").Size
local function OnTouched()
local expl = Instance.new("Explosion")
expl.Parent = mine
expl.Position = mine.Position
wait(1)
mine:Remove()
end
mine.Touched:connect(OnTouched)
end
createmine()
end
wait(3)
function OnTouched()
local expl = Instance.new("Explosion")
expl.Parent = part
expl.Position = expl.Parent.Position
wait(1)
part:Remove()
end
part.Touched:connect(OnTouched)
end

12 comments
So I've taken the second explosion function out of the while loop, but now the new mine does not explode, and because of the loop is created every three seconds. I thought that because I named the new part the same name as the old one which is checked for in the while loop, it would still work, but it doesn't. Why?
Give me a second to fix this...
The new mine does print "the mine is okay" now, but still does not explode. Is this because in the beginning "part" is game.Workspace.mine? I tried changing it to "game.Workspace:FindFirstChild("mine") but no luck.
If this doesn't work as you want it to, I would advise trying the while loop in a co-routine.
What is a co-routine?
A co-routine (or coroutine) is a part of Lua that allows you to run two different things at the same time in the same script (for instance, two while loops).
Does it not work, by the way?
The script identifies the new mine and says "the mine is okay," but it does not explode on player touched. I've been messing around with functions to get it to work but no such luck.
Is the part removing itself?
^No, just sits there. Odd.
Try the coroutine, and if it works, I'll explain why it does.
It does not. Now, the original mine does not explode. Maybe because it is in the while loop?