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

Help w/ function onTouched?

When I touch the part, my body does not explode.

--Make players heads explode when touched part
leftleg = game.Workspace.Player:GetChildren()
explosion = Instance.new("Explosion")

function onTouched(player)
	player.leftleg = explosion
	
	
	
end

script.Parent.Touched:connect(onTouched)

2 answers

✓ Accepted answer
function onTouch(hit)
	if game.Players:GetPlayerFromCharacter(hit.Parent) then
		local Char = hit.Parent
		local explosion = Instance.new("Explosion", Char.Head)
		explosion.Position = Char.Head.Position
	end
end

script.Parent.Touched:connect(onTouch)

Checks if what touched the Part is indeed a Player's character, then Instances an explosion into that character's head.

I have no idea how to do explosions though.

2 comments

0

It does not make me explode

1

I don't know how to do explosions.

So what we would first do is make the function.

function explode(part)

end

Now we will make a variable to access the torso.

local torso = part.Parent:FindFirstChild("Torso')

We now need to make sure that Torso actually exists, using if ~= nil then.

if torso ~= nil then

Now we do the explosions.

local explosion = Instance.new("Explosion")
explosion.Parent = torso
explosion.Position = torso.Position

The Finished Script

function explode(part)
	local torso = part.Parent:FindFirstChild("Torso')
	if torso ~= nil then
		local explosion = Instance.new("Explosion")
		explosion.Parent = torso
		explosion.Position = torso.Position
	end
end

We do not need to worry about the health, because the explosion will kill them anyways.

Please accept if helped.

Thank you.

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