Tried to make a random table index picker, it does not work

local function Move()
	
	ReachedToDest.Value = false
	script.Parent.Humanoid:MoveTo(TargetNode.Value.Position)
	script.Parent.Humanoid.MoveToFinished:Wait()
	ReachedToDest.Value = true
	
	for _, V in pairs(TargetNode.Value.Targetable:GetChildren()) do
		if V.Name == "One" then

			Nodess.Node1 = V.Value


		elseif V.Name == "Two" then

			Nodess.Node2 = V.Value


		elseif V.Name == "Three" then

			Nodess.Node3 = V.Value


		elseif V.Name == "Four" then


			Nodess.Node4 = V.Value

		end
	end
	print(Nodess)
	local Found = false
	while Found == false do
		TargetNode.Value = Nodess[math.random(1, 4)]
		if TargetNode.Value == nil then
			Found = false
			print("Not Found")
		else
			Found = true
			print("Found")
		end
		task.wait()
	end
	
end

The only part having issues rn is the bottom, where i do the math.random in the table.
It apparently cant find anything in the table event though when i print the table it has things in it

1 Like

TargetNode.Value = Nodess["Node"..math.random(1, 4)]

This is a fix to the existing code. To understand how that works, you have to look at how you were setting values in Nodess: Nodess.Node1 = V.Value. The index “Node1” is a string in the table, acting as a key, but later you attempt to access it with just the number as if it were a list. The fix should instead make a string with “Node” and append the number, so that it can be indexed like a dictionary.

There may be more efficient and less complex methods for choosing randomly.

1 Like

that actually worked, and im really surprised my code (besides that one issue) worked first try

2 Likes

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