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

Nothing is happening when I run this landmine code, what did I do wrong?

What im trying to do is have multiple mines explode when you touch them.

function Touch(part)
	while true do
		touch = 1
		wait(1)
	end
end

function Mine(part)
	if part.BrickColor == BrickColor.Black()then
		explosion = Instance.new("Explosion")
		explosion.Position = part.Position
		explosion.Parent = game.Workspace
		part.BrickColor = BrickColor.White()
	else
		if touch == 1 then
			part.BrickColor = BrickColor.Black()
		end
	end
end

children = game.Workspace:GetChildren()
while true do
	for _, child in pairs(children) do
		if child.Name == "Mine" then
			Mine(child)
		end
	end
	wait(1)
end

Mine = game.Workspace.Mine
Mine.Touched:connect(Touch)

4 comments

0

Any errors? Is "Mine" an Model or what?

0

Also do note that your Mine function is override by the "Mine" variable and I don't see your Mine function called when the part is touched.

0

Mine is a part i have

0

The code works, but it will instantly explode upon playing because it's not looking to make sure that a person is touching it.

2 answers

while true do

children = game.Workspace:GetChildren()	

for _, child in pairs(children) do

		if child.Name == "Mine" then
			child.Touched:Connect(function(hit, part)
			part = child

	if hit.Parent:FindFirstChild("Humanoid") then
		part.BrickColor = BrickColor.Black()

	 if part.BrickColor == BrickColor.Black()then
       		 local explosion = Instance.new("Explosion")
        	 explosion.Position = part.Position
        	 explosion.Parent = game.Workspace
        	 part.BrickColor = BrickColor.White()
		 wait(3)
		 part.BrickColor = BrickColor.Black()
	    	end
	end
end)
		end
	end
	wait(1)
end

Your initial code is not set up for detecting a player. When you want to detect that a player is touching your landmine, you generally need to find if the part that touched the mine contains a Humanoid, which all players have.

4 comments

0

oh ok thank you.

0

I tested it and it worked perfectly but I already knew how to do that. What I wanted was if it would work on multipile "Mine"s. Sorry, I should've made it more clear in the desc.

0

Updated my code block. This should work for multiple Mines now.

0

I think 'children' should be defined inside the while loop. Since his game is probably dynamic, after a bit of time the 'children' table could become outdated..

I always find it easier to place multiple items I want a function on in a folder and script it as

Local folder = workspace:WaitForChild("name of the folder") Local parts = folder:GetChildren()

For _, object in pairs(parts) do --// code here End

that works for me. I'd put that in code block but my phone don't work like that lol

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