Magnitude Combat System

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


This is the code if anyone wants to give it a look. I don’t know how creative the solution would have to be, but i’ll keep trying.

Thanks wugs.

3 Likes

If you dont mind me asking what font is that?

2 Likes

unfortunately its the last font to ever exist, its been removed from the font selection; Just kidding :rofl: its Andale Mono, I find it very easy to look at.

3 Likes

Thanks man haha hope you find a solution soon.

3 Likes

i think this is what your asking for

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)
3 Likes

Thanks, i’ll give this a shot rn

1 Like
  • 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
1 Like

So if the attack range is 5 we use the characters lookvector to workout the attackposition using
characterPosition + lookVector * (attackRange / 2)

then we check the magnitude to see if the enemys are is inside that circle

5 Likes

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

1 Like

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

1 Like

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 !

1 Like

Have you ever heard of a dot product? It prevents exactly what you are talking about Vector3 Dot Product: Calculating NPC Field of View (Roblox Studio) - YouTube

1 Like

yes i use dot product often but if you want a simple solution the code i sent above is about as simple as you can make it

and if you use the dot product it will make a cone shape not a circle shape like mine

2 Likes

sure i have a Discord and a YouTube where you can find me

1 Like