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:
01 | function 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 |
07 | end |
08 |
09 | function expscript() |
10 | local mine = game.Workspace:FindFirstChild( "mine" ) |
11 | local script = game.ReplicatedStorage:FindFirstChild( "explosion" ) |
12 | script:Clone().Parent = mine |
13 | end |
14 |
15 | while true do |
And here is the one being added:
1 | function 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() |
7 | end |
8 |
9 | script.Parent.Touched:connect(OnTouched) |
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:
01 | function OnTouched(Part) |
02 | if 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() |
08 | end |
09 | end |
10 |
11 | script.Parent.Touched:connect(OnTouched) |
Hope it helps!