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

Why does this explode without being touched?

Asked by 8 years ago

I have two scripts, one that creates and adds the script to the part, and one that explodes the part on touched. Why does the part explode instantly until being removed?

Here is the first:

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

function expscript()
    local mine = game.Workspace:FindFirstChild("mine")
    local script = game.ReplicatedStorage:FindFirstChild("explosion")
    script:Clone().Parent = mine
end

while true do
    wait(2)
if game.Workspace:FindFirstChild("mine") == nil then
    createmine()
    expscript()
end
wait(2)
end

And here is the one being added:

function OnTouched()
    local expl = Instance.new("Explosion")
    expl.Position = script.Parent.Position
    expl.Parent = script.Parent
    wait(1)
    script.Parent:Remove()
end

script.Parent.Touched:connect(OnTouched)

1 answer

Log in to vote
2
Answered by 8 years ago

The OnTouched() is Fired whenever the Mine touches ANY part, not just a Character. If you wan't it to be Character only, then you have to search something, that is unique only for a Character, which is Humanoid. This is how the script should look like:

function OnTouched(Part)
if Part.Parent:FindFirstChild("Humanoid") then
    local expl = Instance.new("Explosion")
    expl.Position = script.Parent.Position
    expl.Parent = script.Parent
    wait(1)
    script.Parent:Remove()
end
end

script.Parent.Touched:connect(OnTouched)

Hope it helps!

0
Ah, thanks! DrCylonide 158 — 8y
Ad

Answer this question