Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 7 years ago
Edited 7 years ago

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)
0
Any errors? Is "Mine" an Model or what? antonio6643 426 — 7y
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. antonio6643 426 — 7y
0
Mine is a part i have SmugNyan 24 — 7y
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. block01352 57 — 7y

2 answers

Log in to vote
0
Answered by 7 years ago
Edited 7 years ago
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.

0
oh ok thank you. SmugNyan 24 — 7y
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. SmugNyan 24 — 7y
0
Updated my code block. This should work for multiple Mines now. block01352 57 — 7y
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.. Goulstem 8144 — 7y
Ad
Log in to vote
0
Answered by 7 years ago

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

Answer this question