I am making a combat system that is magnitude base. The current issues that have been expressed on the forum about magnitude base combat are the following;
[ You have to loop through the workspace]
[The magnitude only fires once] (Which is what I want so its fine)
[You can hit the players behind you]
These all have solutions i’ve already found the answers to, with tools such as dot product, or global tables to put tagged characters in etc.
Now my current dilemma is, when doing the range check that checks if any of the tagged characters or (dummys in this case) are in say 5 studs of the plrs humanoid root part, we damage the dummy that is within that range. The issue is, when 2 dummys are directly side by side(to the point that their arms are right next to each other), They both fulfill the range condition, & as a result of that, the dummy that has an earlier position inside of the character table (lets call it dummy1) is the character that is prioritized even though I am closer to the dummy2.
Incase my issue isnt clear, I want to find out how to make specify to affect the closer dummy even though 2 dummies satisfy the initial range condition.
I have tried things like putting the distances of the dummies that fulfill that range check to the plr in another table, then sorting the table and making it affect the 1st child in the table being that its from min to max, however, that didn’t work. I couldn’t figure out how to use math.min for the table either since unpack(table) didn’t work inside math.min
local attackRange = 5
local halfAttackRange = attackRange / 2
local characterPosition = character.PrimaryPart.Position
local characterLookVector = character.PrimaryPart.CFrame.LookVector
local attackPosition = characterPosition + characterLookVector * halfAttackRange
-- table where we will store all dummy's in attack range
local dummys = {}
-- find dummys that are inside the attack range
for i, dummy in ipairs(workspace.Dummys) do
-- this dummy is out of attack range skip
if (dummy.PrimaryPart.Position - attackPosition).Magnitude > halfAttackRange then continue end
-- add dummy to dummy's table
table.insert(dummys, dummy)
end
-- if there are no dummys in attack range exit
if #dummys == 0 then return end
-- now lets loop all dummys in attack range and find the one closest to the character
local closestDummy = nil
local closestMagnitude = math.huge
for i, dummy in ipairs(dummys) do
local magnitude = (dummy.PrimaryPart.Position - characterPosition).Magnitude
if magnitude >= closestMagnitude then continue end
closestDummy = dummy
closestMagnitude = magnitude
end
-- we found the dummy
print(closestDummy, closestMagnitude)
It worked perfectly thank you, Id like to understand why you got the halfattkRange (by dividing the attack range by 2
why you did that math with the charPos, charLookVector and the halfAttkRange. I understand it gets the attack Position but how does the look vector and half attack range play a part in that
& Why did you use ipairs instead of regular in pairs
The 2.5 signifies the attkRange/2. Its the radius of the circle? What im understanding is, we have an attack range 5. Then to get the attack position or something like a hitbox, we add the character position to the look vector. Then multiply that by half of the attack range as some kind of radius? Im prob not familiar with arithmetic with vectors
yes 2.5 is the radius of the circle and that’s 50% of the attack range
so we are moving the characters position forward by 50% of the attack range so the circle is in front of the character if we never moved it forward then we would get enemy’s behind the character and we don’t want that
oooooh I seeee thanks man. I’m really trying to advance as a developer and it would really be helpful if I could speak to u on discord for when I encounter situations such as this one? Only if ur comfortable with that. Thanks !