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

i know this post exists, but i really didn’t wanna bump it

i have this code:

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

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

see that enemy variable?

that variable gets created for each ray that hits the humanoid

now, i also have a damage variable at the top of the script, which is 11
and i want to have a variable called totalDamageor something, that is basically damage * enemyAmount

since i can’t multiply damage by enemy (since enemy is a humanoid, and humanoid is not a number), i came up with the idea to store the variables in a table, then multiply the damage by the amount of instances in the table

then clear the table after everything is done

but the main problem is, i’ve got no clue how to get the amount of instances in a table
now the post i linked above does have an answer in it, but to be honest i really don’t understand it

Just for clarification:

  1. Enemy is added to a table for each ray that hits the humanoid.

  2. You want to get the number of “Enemy” in that table.

Is that correct?

3 Likes
  1. yes

  2. the number of “enemies” in that table, in other words the amount of humanoids
    (instances was really not a good word to use)

In that case, here’s what you can do:

local tabel = {...}; -- your table

for _,v in tabel do
	if v:IsA("Humanoid") then
		-- code here
	end
end

Where “v” is the instance that you’re looking for.

2 Likes

this isn’t necessarily what i was going for

i mean, i’ll still try it

but i was more going for

if enemy.Instance.Parent:FindFirstChild("Humanoid") then
table.insert(table, enemy)

-- some sort of function/loop that returns the amount of humanoids in that table

local totalDamage = damage * returnedValue

enemy:TakeDamage(damage)
event:FireClient(player, totalDamage, enemy)
table.remove(table, table:GetChildren()) -- is this the correct way to do it???

I see, if that’s what you’re looking for then I believe a little bit of playing around with my code can help out:

if enemy.Instance.Parent:FindFirstChild("Humanoid") then
table.insert(table, enemy)

-- some sort of function/loop that returns the amount of humanoids in that table
local foo = 0;

for _,v in YOUR_TABLE do
	if v:IsA("Humanoid") then
		foo += 1;
	end
end

local totalDamage = damage * returnedValue

enemy:TakeDamage(damage)
event:FireClient(player, totalDamage, enemy)

Also, the table.remove is not the correct way to do it. You need to get the value in the table in order to remove. This might be a little repetitive but I’m sure you can find a way to play around with it:

if enemy.Instance.Parent:FindFirstChild("Humanoid") then
table.insert(table, enemy)

-- some sort of function/loop that returns the amount of humanoids in that table
local foo = 0;

for _,v in YOUR_TABLE do
	if v:IsA("Humanoid") then
		foo += 1;
	end
end

local totalDamage = damage * returnedValue

enemy:TakeDamage(damage)
event:FireClient(player, totalDamage, enemy)

for _,v in YOUR_TABLE do
	if v:IsA("Humanoid") then
		table.remove(YOUR_TABLE, v);
	end
end
1 Like

If i understand what you want correctly, you can just use #tablename to return the number of instances in the table.

3 Likes

I overcomplicated it.

Use @Polygonizised’s method :sob:

2 Likes

huh? can you elaborate? stupid character limit ijushgdf hsdfgjioup hsgfojiupshfgdoiuj

For example,

local stuff = {workspace.Part, workspace.Part2}

print(stuff) -- this will print what is in the table, we do not want that. Instead,
print(#stuff) -- this will print 2, as there is 2 instances in the table of "stuff".
2 Likes

this doesn’t quite seem to do the trick

i’ve tried 2 methods (1 will be commented)

local enemyTable = {}

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
			
			table.insert(enemyTable, enemy) -- local enemyTable = {enemy}
			
			local pelletsHit = #enemyTable
			print(pelletsHit) -- this will always print out 1, sometimes 2

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

Could you try instead of using a local variable of the number of instances, simply use print(#enemyTable)? Also, are you sure that the value isn’t just 1 or 2?

doing print(#enemyTable) seems to +1 the value with each shot (shot, not ray)

this will reset after 2 seconds from what i’m seeing

i assume value is the amount of humanoids?

Yes. I don’t understand why this wouldn’t be working, but the answer might be right in front of me. I’m not the best of coders out there.

isn’t programming just fun and amazing

1 Like

This is happening due to it being inside the “result ~= nil” if statement.

Every time the pellet is hit, it adds the humanoid or instance into the table, but you also have code in there which deals with what happens when the pellet is hit. Hence, why you’re getting #tablename +1 every time.

You can try returning the table when the for loop ends and then deal with the damage there.

1 Like

i’m not exactly following, what do you mean by this?

Been a while since I used raycast but pretty sure it only detects one instance at a time and can’t detect multiple like you’re trying to achieve.

Are you saying you want to cast the rays first and deal the damage after exiting the for loop? Honestly the way you have it set up seems like it should work just fine.

what is this replying to? :sob: CHARACTER LIMIT!!!