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 9 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:

01function createmine()
02    local mine = Instance.new("Part")
03    mine.Parent = workspace
04    mine.Position = game.Workspace:FindFirstChild("master").Position
05    mine.Name = "mine"
06    mine.Size = game.Workspace:FindFirstChild("master").Size
07end
08 
09function expscript()
10    local mine = game.Workspace:FindFirstChild("mine")
11    local script = game.ReplicatedStorage:FindFirstChild("explosion")
12    script:Clone().Parent = mine
13end
14 
15while true do
View all 22 lines...

And here is the one being added:

1function OnTouched()
2    local expl = Instance.new("Explosion")
3    expl.Position = script.Parent.Position
4    expl.Parent = script.Parent
5    wait(1)
6    script.Parent:Remove()
7end
8 
9script.Parent.Touched:connect(OnTouched)

1 answer

Log in to vote
2
Answered by 9 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:

01function OnTouched(Part)
02if Part.Parent:FindFirstChild("Humanoid") then
03    local expl = Instance.new("Explosion")
04    expl.Position = script.Parent.Position
05    expl.Parent = script.Parent
06    wait(1)
07    script.Parent:Remove()
08end
09end
10 
11script.Parent.Touched:connect(OnTouched)

Hope it helps!

0
Ah, thanks! DrCylonide 158 — 9y
Ad

Answer this question