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
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
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".
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
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?
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.
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.