Your original post, I honestly don’t see why you need to create a table in the first place. Your code looks like it should deal the correct amount of damage to someone. It should work fine as is. Creating a table just complicates it even more, as if you took the number of instances in the table and multiplied it by the damage variable you’re assuming all the humanoids in that table are the same humanoid. What if a single (I assume this is a shotgun) shotgun blast causes one player to be hit by 3 pellets, but another to be hit by 1? Both players would take 44 damage with a table setup which is already redundant in the first place.
I’m finding it hard to understand what you’re trying to achieve even after reading it all Give me a quick rundown and I can most likely find the answer.
No if you put TotalDamageDealt at the above the loop like you did with the table and then added the damage like I specified where you usually insert stuff into the table it should work.
Not at all. Since you’re running a for loop, every time damage is dealt, the “TotalDamageDealt += Damage” will run per amount of times the for loop executes. In your case, it seems to be declared at the start as “PELLETS”.
Alright so if I’m understanding you correctly, each time this weapon is fired, you want to tell the client who they hit, and what amount of damage they dealt to them.
I would just create a dictionary (which is a table but with indexes that are anything other than numbers).
local humanoidsHit = {}
for pellet=1, PELLETS do
local spreadX = HORIZONTAL_SPREAD.min + (math.random() * (HORIZONTAL_SPREAD.max - HORIZONTAL_SPREAD.min))
local spreadY = VERTICAL_SPREAD.min + (math.random() * (VERTICAL_SPREAD.max - VERTICAL_SPREAD.min))
local direction = (mouse.Direction.Unit + Vector3.new(spreadX, spreadY)).Unit
local displacement = direction * MAX_DISTANCE
local result = workspace:Raycast(
origin.Position,
displacement,
raycastParams
)
local tracer = Instance.new("Part")
tracer.Parent = workspace
tracer.Anchored = true
tracer.CFrame = CFrame.lookAt(origin.Position + (displacement / 2), origin.Position + displacement)
tracer.Size = Vector3.new(0.1, 0.1, displacement.Magnitude)
tracer.Transparency = 0.6
tracer.CanCollide = false
tracer.Color = Color3.new(0.960784, 0.666667, 0.156863)
game:GetService("Debris"):AddItem(tracer, 0.2)
if result ~= nil then
local enemy = result.Instance.Parent:FindFirstChild("Humanoid")
if enemy == game:GetService("Players"):GetPlayerFromCharacter(result.Instance.Parent) then continue end
local totalDamage = humanoidsHit[enemy]
if (not totalDamage) then
humanoidsHit[enemy] = damage
else
humanoidsHit[enemy] += damage
end
end
end
for humanoid, totalDamage in pairs(humanoidsHit) do
humanoid:TakeDamage(totalDamage)
DamageGUIEvent:FireClient(player, damage, enemy)
end
humanoidsHit = {}
By the time the for loop finishes executing, you should have a table/dictionary that looks something like this:
{Humanoid = 44, Humanoid = 22, Humanoid = 33}
Each humanoid that was hit by the shotgun blast (I’m assuming that’s what this is) will have a number assigned to them based on the number of times they were hit by a raycast. You can go through the dictionary and damage each of them with the correct amount of damage as well as inform the client of which humanoid they just damaged and how much damage they dealt to them.