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

Custom Explosion Not Dealing Damage?

I am trying to make an explosion where the player is, and it is custom shaped. But it won't do damage. Here is my code:

local function explode(part,givencolor,radius)
	local explosion = Instance.new("Part", part.Parent)
	local explosionSounds = {
		"rbxassetid://144699494",
		"rbxassetid://2648563122",
		"rbxassetid://3154829820",
		"rbxassetid://1197889183"
	}
	explosion.Anchored = true
	explosion.Position = part.Position
	explosion.Shape = Enum.PartType.Ball
	explosion.Material = Enum.Material.Neon
	explosion.CanCollide = false
	explosion.Size = Vector3.new(1,1,1)
	explosion.Color = givencolor
	explosion.Name = "Explosion"
	local exSound = Instance.new("Sound",explosion)
	exSound.SoundId = explosionSounds[math.random(#explosionSounds)]
	exSound.Volume = 5
	exSound:Play()
	for i=1, radius, 1 do
		explosion.Size = explosion.Size + Vector3.new(1,1,1)
		explosion.Transparency = explosion.Transparency + 1/radius
		explosion.Touched:Connect(function(hit)
			local humanoid = hit.Parent:FindFirstChild("Humanoid")
			if humanoid and hit.Parent ~= explosion.Parent then
				humanoid:TakeDamage(5)
			end
		end)
		wait()
	end
	explosion:Destroy()
end

Can anyone help me (pls don't just comment, i need ANSWERS.)

1 comment

0
  1. Check for errors, 2. Add print statements after anything that might pause/stop the script (ex: if statements) then comment back anything you get. We don't have enough info on whats not happening

2 answers

Why don't you just use Magnitude? Trying to rely on Touched is unreliable, since Touched tends to be really buggy and laggy.

Your Magnitude should look something like this:

(origin - target).Magnitude

Where:

  • origin is the Vector3 to start from.

  • target is the Vector3 to end at.

In terms of Vector3 values, you should use the Position of the part and the Position of whatever player you're trying to hit with the explosion. If you want to obtain a player, use GetPlayerFromCharacter() on any Model within the Workspace.

Also, if you're working locally, you should verify that the player who fired the explosion does not take any damage; this can be done on the server using RemoteEvent:FireServer()'s or RemoteFunction:InvokeServer()'s passed player argument, which is the player who fired the event.

4 comments

0

again, kinda not what im looking for. there isn't supposed to be a target, it's just supposed to damage anyone who is near the player.

0

The players near the player firing the explosion are the targets. You're not thinking of this correctly.

0

You can have infinite targets with this method as long as you run the magnitude for all of them. This is most likely going to be the best answer.

0

Using magnatude in a loop didn't work.

Try this for the taking damage part:

script.Parent.Touched:Connect(function(Hit) --You can edit this part as well
	if Hit and Hit.Parent:FindFirstChild("Humanoid") then
		Hit.Parent.Humanoid.Health = 0 --You can customize this number
	end
end)

2 comments

0

but that just kills me! im trying to make an explosion attack that only hurts other players, not the player who activated it.

0

Oh well that's more difficult to make.

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