I forgot how to do it. I need my script to not act if the parent of the hit object is nil, because otherwise it returns an error.
Here's my script.
01 | function onTouched(hit) |
02 | if hit.Parent.Name = = "Monorail" then |
03 | local explosion = Instance.new( "Explosion" ) |
04 | explosion.Parent = hit |
05 | explosion.Position = hit.Position |
06 | wait( 0.5 ) |
07 | hit:remove() |
08 | wait( 5 ) |
09 | hit.Parent:remove() |
10 | end |
11 | end |
12 |
13 | script.Parent.Touched:connect(onTouched) |
I don't think the lack of parent is the problem, it is because hit is removed, so it no longer has a parent, because it no longer exists, because you removed it. To fix this make a variable of the parent before hit is removed.
01 | function onTouched(hit) |
02 | if hit.Parent.Name = = "Monorail" then |
03 | local explosion = Instance.new( "Explosion" ) |
04 | explosion.Parent = hit |
05 | explosion.Position = hit.Position |
06 | wait( 0.5 ) |
07 | local parent = hit.Parent -- This should do it |
08 | hit:remove() |
09 | wait( 5 ) |
10 | parent:remove() |
11 | end |
12 | end |
13 |
14 | script.Parent.Touched:connect(onTouched) |