How To Get a math.min() Outcome Using Number Values from a Dictionary

I’m trying to make a Dictionary of position values produce an outcome using math.min(), but I don’t think that will work. How would I do that? I’m making a Ring Light Dash that you can find in most Modern Sonic games. If someone could help me with that completely, that would be even better. Thx in advance.

Need more information / general clarification on your outcome.

1 Like

Sooo something like

dict = {
    position1 = Vector3.new(),
    position2 = Vector3.new(),
    ...
}

???

Can’t figure out what a ring light dash is from googling, either?

Hello.

If you have a list or a dictionary and you want to find the smallest vector, you can do the following

local function getMinVector(t)
    local bestVector
    local maxLength = math.huge
    for i, v in next, t do
        local length = v.Magnitude
        if length < maxLength then
            maxLength = length
            bestVector = v
        end
    end
    return bestVector
end

Keep in mind that this is supposing your list only contains vectors as values.

Best regards,
Octonions

(Props to everyone for finding this quickly) The outcome I was hoping for is to be able to find the closest Ring to dash to, using math.min() for calculations on which is the closest. Also, a “Ring Light Dash” that I was talking about is referring to this:
Light Dash

Yes, I know he’s T-posing, I’m too lazy for anims right now.

To @Octonions, yes, I will be using a Vector3 that is the difference between the HumanoidRootPart.Position and the Ring position. To @ThanksRoBama, a “Ring Light Dash” is, in the Sonic series, just called a Light Dash (with the first game it was in, Sonic Adventure 1, calling it the Light Speed Dash). To @xZylter, look at the top of this post.

1 Like

Can you not just have the first ring named ‘StartRing’ or something?

1 Like

Okay, that makes lots more sense then :stuck_out_tongue: Game already looks cool BTW.

If you’re going to have 100s or even 1000s of rings in your game, then looping through all of them to compute and compare the distance to the character might be too slow. If there’s a range limit then you can easily improve it by only comparing the rings that are within that range:

local TagS = game:GetService("CollectionService")

local RING_OVERLAP_PARAMS = OverlapParams.new()
RING_OVERLAP_PARAMS.FilterType = Enum.RaycastFilterType.Whitelist

local ringOverlapParamsUpToDate = false

function findNearestRingWithinRadius(point: Vector3, radius: number, overlapParams: OverlapParams?): Model?
    if not ringOverlapParamsUpToDate then
        --This only needs to be done if there's actually been added any rings since we last checked.
        --Slight optimization, don't know if it matters. The important thing is what comes after...
        updateRingOverlapParams()
    end
    
    local partsInRadius = game.Workspace:GetPartBoundsInRadius(point, radius, overlapParams)
    local nearestPart = getNearestPV(point, partsInRadius)
    return nearestPart.Parent --Return the ring itself, not it's PrimaryPart
end

function updateRingOverlapParams()
    RING_OVERLAP_PARAMS.FilterDescendantInstances = TagS:GetTagged("RingPrimaryPart")
    ringOverlapParamsUpToDate = true
end

function getNearestPV(point: Vector3, pvInstances: {PVInstance}): PVInstance?
    local nearest, nearestDist = nil, math.huge
    for _, pv in ipairs(pvInstances) do
        local dist = (pv:GetPivot().Position - point).Magnitude
        if dist < nearestDist then
            nearest, nearestDist = pv, dist
        end
    end
    return nearest
end

TagS:GetInstanceAddedSignal("RingPrimaryPart"):Connect(function()
    ringOverlapParamsUpToDate = false
end)

Using this approach, there shouldn’t be any limit on how many rings you can have. Or at least, the limit doesn’t come from having to find the nearest ring. Even if there’s 1,000,000 rings, this code only looks through the few rings that are actually in range. I think Roblox’s spatial queries (like GetPartBoundsInRadius) are pretty efficient and critically don’t scale with the total number of parts in game. It probably uses a spatial partitioning data structure like octtrees.

The problem with that is the fact that you can start Light Dashing from any point. If you collect some Rings (including the “StartRing”) and then initiate it, then it won’t work, even though you should be able to. This is now because the StartRing is gone.

Thx. I’m calling it, Sonic: Dark Moon.

Should happen eventually, as “dying” doesn’t reset the rings and I’m going to add that

You may or may not have heard of this engine, but I’m using the Sonic Sound Engine. This currently doesn’t have PrimaryParts but I’ll try and see if it doesn’t break it.

Last note:

"RingPrimaryPart"
Would that be the name of the part (currently just named “ring,” again, because of the Sonic Sound Engine) to check for?
Again, thx for positive feedback and awaiting your reply!

TBH you may have saved my bacon on this

1 Like

Nope, not the name but a tag that I figured would be a good way to organize rings. Look up CollectionService on the wiki if you’re not familiar with tags, they’re great.

Ah, ok. One more thing, there’s nothing adding the Rings to the tag. How and where would I add it?

1 Like

Use a tag management plugin and just set the tag on the prefab Ring model you have in ServerStorage or ReplicatedStorage or w/e, the one that you clone in to the game when rings spawn.

:neutral_face: uhhhhhhhh I don’t have tha- Ok… That actually sounds better than what I have right now. Right now, I have Rings that would be easy to place, like a straight line, in the Workspace. I also have a Ring in ReplicatedStorage that I use for the ones in the picture, those being position calculated whatnot using this I found:

But all of them in RepStorage sounds way better.

1 Like

Oh sorry, that’s just how I usually do things. If you have lots of rings placed around the game world already, you could also just select all of them and tag them all at once using a tag management plugin.

I think I’m just gonna use it in RepStorage. I’m literally working the kinks out of rewriting that, now.

1 Like

do you want to use NumberValues, if you want, then use <NumberValueInstance>.Value Replace <NumberValueInstance> with the variable or the path of the NumberValue

Ok I’ll think of that if I need to use it again, because I already finished what I was trying to do using @ThanksRoBama 's method. I haven’t “Solution’ed” it yet because I just got it working.

Good news! I HAVE FIGURED IT OUT! @ThanksRoBama was the one who helped me! Thank you for all your helpful posts! I think there are too many !'s but I don’t care!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.