Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How to make this part explode only when it collides with objects?

I made a script where the part explodes when it collides with anything. How do I make so it only explodes when touching the objects, not with players?

Here's my script;

function onTouched(hit) 
	local e = Instance.new("Explosion") 
	
	e.BlastRadius = 15 

	e.BlastPressure = 1000

	e.Parent = game.Workspace 

	e.Position = script.Parent.Position 
end

script.Parent.Touched:connect(onTouched)

4 comments

0

check that hit.Parent does not contain a humanoid

0

What do you mean by that?

0

Check if hit.Parent has 'Humanoid' in an if statement at the beginning of the function.

0

if humanoid then end if not humanoid then local e...

1 answer

Add a check for humanoids. Also, add a debounce to not lag the player's game.

local debounce = true
function onTouched(hit) 
if hit.Parent:FindFirstChild("Humanoid") then
print('Humanoid found.')
else
	if debounce == true then
		debounce = false
   		local e = Instance.new("Explosion") 

    	 e.BlastRadius = 15 

		 e.BlastPressure = 1000

   		 e.Parent = game.Workspace 

    	 e.Position = script.Parent.Position
			wait(1)
			debounce = true
	else
		print('deb is false lolololol')
		end
	end 
end

script.Parent.Touched:connect(onTouched)

This discussion is preserved as it was. New answers, comments, and votes are closed.