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

Attack Only deals damage to closest user?

The attack only deals damage to the closest player near it but even the people that are in the attack don't get damaged only the closest player does this is the code

local Explosion = game.Lighting.ExplosionEffect:Clone()
Explosion.Parent = game.Workspace
Explosion.Position = player.Character.UpperTorso.Position
Explosion.Anchored = true
script.Explosion:Play()
local hum = player.Character.Humanoid
local damaged = true

Explosion.Touched:Connect(function(hit)
    if not damaged then return end
    damaged = false
    local ehum = hit.Parent:FindFirstChild("Humanoid") or hit.Parent.Parent:FindFirstChild("Humanoid")
    if ehum and ehum ~= hum  then
    ehum:TakeDamage(40)
    wait(1)
    end    

    damaged = true


end)

for i=1, 10 do

Explosion.Size = Explosion.Size + Vector3.new(19,19,19)

wait()


end

Explosion:Destroy()

4 comments

0

Is this in Filtering Enabled?

0

Yes

0

Are you trying to create some sort of grenade or bomb of such?

0

Its a explosion magic attack.

1 answer

I think it's because of the way you use debounce. You set debounce for any occurrences of Touched event, meaning after the first player is damaged, other player would have to touch the explosion one second later to get damaged.

So what I suggest you to do is for example store the players names or humanoids into a debounce table and then remove them afterwards.

local Explosion = game.Lighting.ExplosionEffect:Clone()
Explosion.Parent = game.Workspace
Explosion.Position = player.Character.UpperTorso.Position
Explosion.Anchored = true
script.Explosion:Play()
local hum = player.Character.Humanoid
local damaged = {}

Explosion.Touched:Connect(function(hit)
	local ehum = hit.Parent:FindFirstChild("Humanoid") or hit.Parent.Parent:FindFirstChild("Humanoid")
	if ehum and ehum ~= hum  then
		if damaged[hit.Parent.Name] then
			return
		end
		damaged[hit.Parent.Name] = true
		ehum:TakeDamage(40)
		wait(1)
		damaged[hit.Parent.Name] = nil
	end
end)

for i=1, 10 do
	Explosion.Size = Explosion.Size + Vector3.new(19,19,19)
	wait()
end

Explosion:Destroy()

3 comments

0

I get an error saying "ServerScriptService.Explosion:25: attempt to index upvalue 'damaged' (a boolean value)"

0

Notice line 7: local damaged = {}

0

It still only hits the closest player but not the other ones trapped in the explosion.

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