Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
2

Best way to handle detecting when in range to a list of objects?

Asked by 8 years ago
Edited 8 years ago

Basically I'm creating a system where when you walk up to certain parts, if your character is within range, a prompt will show up. For the sake of this question you don't have to worry about what the prompt will do, I'm just a bit confused on how I'd handle setting this up.

Info:

The method I have so far is creating a table, containing columns of other tables with information about the part the player needs to be within range of. Like this:

01local stones = {
02    {
03        Name = "Green stone", -- The name
04        Range = 5, -- Distance you need to be from stone to activate it
05        Object = workspace.GreenStone -- The physical stone to get it's position
06    },
07    -- Same thing goes for the rest below
08    {
09        Name = "Red stone",
10        Range = 5,
11        Object = workspace.RedStone
12    },
13    {
14        Name = "Blue stone",
15         Range = 10,
16        Object = workspace.BlueStone
17    }
18}

Now I like this way of storing the objects the player can interact with, but my method for CHECKING if the player is in range of them concerns me a bit.


I'm using magnitude to obviously check the distance between the objects and the player, but the entire process is done within a while loop, iterating the table and comparing the player's current position to each individual objects each iteration. This - I don't like. Here would be an example:

01function updateRange(player)
02    local torso = player.Character:WaitForChild("Torso")
03    for i = 1,#stones do
04        local v = stones[i]
05        if (v.Object.Position-torso.Position).magnitude < v.Range then -- If the player is in range of the current object the for loop is checking
06 
07            print("Player is in range of: ", v.Name) -- Then we'll just say i does this.
08        end
09    end
10end
11 
12-- The while loop that updates it
13while wait(0.5) do
14    updateRange(player) -- We'll just assume player is defined as a player
15end

This method seems buggy, impractical, inefficient, and I don't like it. So my question summed up is pretty much:

Is there a better way for this program to allow you to interact with the parts in the table when in range, without the need for a while loop updating the entire process each time?

If anyone could help with this, I'd really appreciate it. Thanks for reading.

1 answer

Log in to vote
0
Answered by 8 years ago

run updateRange() when you move the Torso trought the Changed event and have the property as "Position"

so like:

1torso.Changed:connect(function(prop)
2if prop=="Position" then
3updateRange(player)
4end
5end)
0
My problem isn't speed, it's efficiency. This would actually be worse. MightyQuestionAsker 297 — 8y
Ad

Answer this question