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

Script doesn't insert explosion?

This script's meant to insert an explosion when you touch it. I was pretty sloppy with finding the humanoid so I'm guessing that's what went wrong, and I'd like some help please?

E = Instance.new("Explosion",workspace)
script.Parent.Touched:Connect(function(hit)
if hit.Parent.IsA("Humanoid") then
E.Parent = game.Workspace.Union

end
end)

2 answers

In your script, you've already defined the parent of 'E' as 'workspace', so there's no point in setting its parent again if the explosion's already gone. Instead, just leave it in empty space.

E = Instance.new("Explosion")

On your 'if' statement, :IsA() is a method to determine its class. The parent of whatever hits the brick is usually a BasePart of some sort.

So rather than using :IsA(), I would verify that 'hit' is a character by using the :GetPlayerFromCharacter() method.

if game.Players:GetPlayerFromCharacter(hit.Parent) then

Also, regarding explosions, it has a convenient property called "Position". You may set the position of the explosion to whatever 'hit' is.

E = Instance.new("Explosion")
script.Parent.Touched:Connect(function(hit)
	if game.Players:GetPlayerFromCharacter(hit.Parent) then
		E.Position = hit.Position
		E.Parent = workspace
	end
end)

Lastly, there's a logic error in this script. If you want the script to spawn an explosion every time the function is invoked, then place variable 'E' inside your script.

script.Parent.Touched:Connect(function(hit)
	if game.Players:GetPlayerFromCharacter(hit.Parent) then
		local E = Instance.new("Explosion")

You can also clone variable 'E'.

E = Instance.new("Explosion")
script.Parent.Touched:Connect(function(hit)
	if game.Players:GetPlayerFromCharacter(hit.Parent) then
		X = E:Clone()
		X.Position = hit.Position
		X.Parent = workspace

Because if the variable is left outside with no clone on the inside, then, when the script runs, the explosion will only happen once.

Hi there, I hope this will help:

Line 1 : The explosion is only created once! Line 3 : Not a valid way of getting a humanoid! Line 4 : E == nil

Fixed code:

local Union = game.Workspace:WaitForChild('Union') -- Wait for the union!

script.Parent.Touched:connect(function(hit)
	if hit and hit.Parent then -- Error checking...
		local Humanoid = hit.Parent:FindFirstChild('Humanoid') -- To check if the object has a humanoid...
		if Humanoid then -- Is it there?
			local E = Instance.new('Explosion', Union) -- Create the explosion!
		end
	end
end)

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