Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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

✓ Accepted answer

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
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.

1 comment

0

what I want to know is how to figure out what part the character is moving to

This discussion is preserved as it was. New answers, comments, and votes are closed.