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

Why does this script not function properly?

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

1 answer

Put your functions and event outside of the while loop.

part = nil -- Setting an empty variable for now

if game.Workspace:FindFirstChild("mine") then
	part = game.Workspace.mine
end

function createmine()
	local mine = Instance.new("Part")
	mine.Name = "mine"
	mine.Parent = game.Workspace
	master = game.Workspace:FindFirstChild("master")
	mine.Position = master.Position
	mine.Size = master.Size
	part = mine
end

while wait(3) do
	if part then
		print("The mine is okay")
	else
		createmine()
	end
end

partCoroutine = coroutine.resume(coroutine.create(function()
	while part do
		part.Touched:connect(function()
			local expl = Instance.new("Explosion")
			expl.Parent = part
			expl.Position = part.Position
			wait(1)
			part:Remove()
			part = nil
		end)
	wait()
	end
end))

12 comments

0

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?

0

Give me a second to fix this...

0

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.

0

If this doesn't work as you want it to, I would advise trying the while loop in a co-routine.

0

What is a co-routine?

0

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

0

Does it not work, by the way?

0

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.

0

Is the part removing itself?

0

^No, just sits there. Odd.

0

Try the coroutine, and if it works, I'll explain why it does.

0

It does not. Now, the original mine does not explode. Maybe because it is in the while loop?

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