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

How do I write to the chosen object with math.random?

Asked by
Prioxis 673 Moderation Voter
9 years ago

So i'm making a NPC with a randomized pattern with a set of nodes I placed and i'm using magnitude to check where he is before he moves again I have my script

while wait() do
local points = game.Workspace.Nodes:GetChildren()
local Table = {}
local Minion = game.Workspace.NPC
Minion:MoveTo(math.random(1,#points)) -- where I don't know what to call
        repeat
            wait()
        until (WhateverPoint.Position - torso.Position).magnitude <= 5 -- line I need help with
end

but I don't know how to write to the chosen object like where WhateverPoint I want that to be whatever node was chosen

2 answers

Log in to vote
0
Answered by
ImageLabel 1541 Moderation Voter
9 years ago

math.random(1,#points) simply returns a randomly generated number between the first argument and the second. This number can also be considered the index in the table at which the random value would be located.

You can find that value a few ways, including..

-- 1. As Benda567 said .. You could either directly index the table like so

table = {'a', 'b', 'c', 'd', 'e'}
print(table[math.random(1, #table)]) -- random letter from `table`

-- 2. However, it would be much more efficient and clean to assign it to a variable, so that you may use said variable later on without having to go through the process again.. which would more than likely return a different value.

table = {'a', 'b', 'c', 'd', 'e'}
value = table[math.random(1, #table)]
print(value) -- random letter from `table`

-- or

table = {'a', 'b', 'c', 'd', 'e'}
value = math.random(1, #table)
print(table[value]) -- random letter from `table`

All the above solutions would return the part at the integer returned by math.random... which you can then use as "WhateverPoint".

while wait() do
    local points = game.Workspace.Nodes:GetChildren()
    local randomPoint = points[math.random(1,#points)]
    local Minion = game.Workspace.NPC
    local Table = {}

    Minion:MoveTo(randomPoint )
    repeat wait() until (Minion.Torso.Position - randomPoint.Position).magnitude <= 5 
end
Ad
Log in to vote
0
Answered by 9 years ago
while wait() do
local points = game.Workspace.Nodes:GetChildren()
local Table = {}
local Minion = game.Workspace.NPC
Minion:MoveTo(points[math.random(1,#points)].Position)
repeat wait() until (Minion.Torso.Position - torso.Position).magnitude <= 5 
end

I think it should look like that, your question is quite confusing. All I know is on line 5 you are trying to move a model to what would be a random number, which wouldn't work.

0
what I want to know is how to figure out what part the character is moving to Prioxis 673 — 9y

Answer this question