PcoWSkbVqDnWTu_dm2ix
We use cookies on this site to enhance your user experience

Parts that Make Players Explode

Parts that Make Players Explode

Jun 23 2018, 2:01 AM PST

Problem

You want to make the person who touches a part explode.

Solution

Use the Explosion object.

Workspace.Part.Touched:connect(function(hit)
	if hit.Parent and game.Players:GetPlayerFromCharacter(hit.Parent) then
		local exp = Instance.new('Explosion')
		exp.Position = hit.Position
		exp.Parent = workspace
	end
end)

Discussion

We start with a standard BasePart/Touched connection and check if hit.Parent is a character, using the Players/GetPlayerFromCharacter method of the Players service. By doing this we can ensure that whatever hit the part was part of a Player (we don’t want to explode when another part touches it, just when a player touches it). Within the conditional we instantiate an Explosion object and parent it to the Workspace. Then we set the BasePart/Position property of the explosion the the Position property of the object that touched the part.

Without setting the Position property correctly, we would get an explosion happening in the center of the map. Positioning will be covered in great detail later on.

Tags:
  • particles