Finding an item with `Math.Random`

So I am trying to get a selection through math.random.

my current code is:

local children = game.ReplicatedStorage.Queue:GetChildren()

local selected = children[math.random(1,#children)]

char = workspace:FindFirstChild(selected)

print(selected)

I try to make it print Selected, but it doesn’t work. Any ideas?

1 Like

Try using selected.Name ? I hope this works?

4 Likes

It’s likely not working/printing anything because you passed an instance instead of a string. FindFirstChild goes off of strings (i.e. the name of the object that you grabbed), which is where the issue might lie.

Try this instead:

local children = game:GetService("ReplicatedStorage").Queue:GetChildren()
local selected = children[math.random(1, #children)]

local char = workspace:FindFirstChild(selected.Name)
if char then
    -- do whatever you want here
end

print(selected)
1 Like

I will try that, I’ll get back to ya!

So it threw the error of: math.random interval argument 2 empty | Invalid field

@Y_Corp

What is inside of ReplicatedStorage.Queue?

Player names that are called from a much larger script, I can send the critical parts if you would like

Could you send a screenshot of what it is? How are the names stored? Are they in StringValues?

Yes. They click a GUI button, then the ui fires a remote to add them to the queue.

The issue is when there is more than 2 people in queue. It somehow broke.

Would you be able to send a file with the critical aspects of that functionality? I’m curious as to why this is happening. Just include the script that is in, the Queue object and its children, and any LocalScripts associated.

Alright so I think I know why.

When using math.random the first number has to be smaller then the second number. Do this to test.

This would make sense to me. I’ll try that, as well as sending some of the aspects

while wait() do
	repeat wait() until #Queue:GetChildren() == 2 and roundStarted.Value == false
	
	
	if #Queue:GetChildren() > 2 then
		local selected = Queue:GetChildren()[math.random(1,#Queue:GetChildren())]
		
		for i, v in pairs(selected) do
			
			roundStarted.Value = true
			
			local player = workspace:FindFirstChild(v.Name)
			
			local pObj = game.Players:GetPlayerFromCharacter(player)
			
			for i, player in pairs(game.Players:GetPlayers()) do
				if player.InQueue.Value == true then
					StartRound:FireClient(player)
					SpawnPlayer(player)
					player.Team = nil
					player:WaitForChild("InQueue").Value = false
					player:WaitForChild("InRound").Value = true
					wait(.1)
					break
				end
			end
			
			StartRound:FireClient(pObj)
			
			wait(5)
			
			pObj.Character.Humanoid.Died:Connect(function()
				if roundStarted.Value == true then
					for i, p in pairs(game.Players:GetPlayers()) do
						if p.InRound.Value == true then
							p:LoadCharacter()
							p.InRound.Value = false
							p.Team = game.Teams.Testers
							roundStarted.Value = false
						else
							return
						end
						
						p.Team = game.Teams.Testers
					end
				end
			end)
		end
	else
		for i, v in pairs(Queue:GetChildren()) do

			roundStarted.Value = true

			local player = workspace:FindFirstChild(v.Name)

			local pObj = game.Players:GetPlayerFromCharacter(player)

			for i, player in pairs(game.Players:GetPlayers()) do
				if player.InQueue.Value == true then
					StartRound:FireClient(player)
					SpawnPlayer(player)
					player.Team = nil
					player:WaitForChild("InQueue").Value = false
					player:WaitForChild("InRound").Value = true
					wait(.1)
					break
				end
			end

			StartRound:FireClient(pObj)

			pObj.Character.Humanoid.Died:Connect(function()
				if roundStarted.Value == true then
					for i, p in pairs(game.Players:GetPlayers()) do
						if p.InRound.Value == true then
							p:LoadCharacter()
							p.InRound.Value = false
							p.Team = game.Teams.Testers
							roundStarted.Value = false
						else
							return
						end
					end
				end
			end)
		end
		
	end	
	
	for i, q in pairs(Queue:GetChildren()) do
		q:Destroy()
	end
	
	for i, v in pairs(spawns) do
		if v[1] == true then
			v[1] = false
		elseif v[2] == true then
			v[2] = false
		end
	end
end

Could you instead send a studio file? Just open up a Baseplate and your game, copy the parts you want to send me into the Baseplate, go to File > Save To File, save it somewhere, then use the “Upload” button (shown in Picture #1).

Picture #1

Then, click upload from my computer, choose the studio file, and send the message (Picture #2)

Picture #2

Queue.rbxl (53.5 KB)

Random script I wrote:

local children = workspace:GetChildren()
local randomSelection = math.random(1, #children)

local selectedChild
for index, child in pairs(children) do
	if index == randomSelection then
		selectedChild = child
	end
end
print("Selected "..selectedChild.Name)

I will look into your code in a minute.

I think it may do that because there are no children. Thus, #children is equal to 0.

Fixed code:

--If this is on the client you need to wait for the children.
local children = game.ReplicatedStorage.Queue:GetChildren()
local numOfChildren = #children

local selected
if numOfChildren > 0 then
    selected = children[math.random(1, numOfChildren)]
end


char = selected -- Selected is an instance, not a string (FindFirstChild(string))

print(selected.Name)

2 Likes

I am not the original guy that asked this, but this is very helpful for like choosing random weapons or items for specific players!