How do I get the amount of instances in a table?

You can handle what happens to the target when it gets hit after the for loops ends.

I think OP has a for loop for every raycast but wants every instance of it.

does this really have to be that complicated

Could you try to take

local pelletsHit = #enemyTable
print(pelletsHit) -- this will always print out 1, sometimes 2

and place this outside the pellet for loop?

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 :sweat_smile: Give me a quick rundown and I can most likely find the answer.

1 Like

image

kind of does the same

i’m really not sure where the numbers come from, especially that 4

1 Like

yeah but now i wanna send the totaldamage dealt to the client, so it can be applied properly on the billboard gui (damage indicator)

1 Like

have a variable that’s damage multiplied by pellets hit (local totalDMG = damage * pelletsHit)

send that variable to the client

having that variable is literally what’s causing this problem

Is there any reason you can’t have a variable called

local TotalDamageDealt = 0 -- Top of the script
TotalDamageDealt += Damage -- inside whenever something gets damage

Then you send TotalDamage over to the client

i can’t, damage is set to 11 at the top of the script

so TotalDamageDealt would just be 11 (unless this doesn’t work the way i think it does)

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.

local TotalDamage = 0

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
			
			TotalDamage += damage

			enemy:TakeDamage(damage)
			DamageGUIEvent:FireClient(player, damage, enemy)
		end
	end

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”.

tried this,

it either prints 11 or 22, evne if all the rays hit the target

this is some space alien problem :sob:

Do you put the print outside the for loop?

no, but i put it outside

and it prints correctly now!! :smiley: (this is a huge step forward)

np, now just make a remote event that sends over that variable and you should be good.

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.

1 Like

yeah… one tiny problem

i need that enemy variable (i need ray.Instance.Parent) and it doesn’t exist outside the for loop

it would appear the raycasts themselves are inconsistent???

i don’t even know what’s going on anymore

all of the rays hit the dummy
1 ray is 11 damage
there are 8 rays, all of them hit
which means 88 damage
the dummy has 80 health

the output prints 11???