Get a random child form a table in a pairs loop

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    i want to insert a random player form a table to a team but I don’t know how to get random object form a table in a pairs loop

  2. What is the issue? Include screenshots / videos if possible!
    I got an error said:
    ServerScriptService.RoundSystem.script.Main:33: attempt to call missing method ‘GetChildren’ of table

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I had tried to find the solution on the forum but I can’t find any thing that I need

local RoundSystemFolder = script.Parent.Parent
local info = require(RoundSystemFolder.Module.RoundInfo) 

local function insertplayertable()
	for i, v in pairs(Players:GetChildren()) do
		table.insert(info.playerstable, 1, v)
	end
end

local function insertteam(toggle)
	local currentinsertteam = "Raiders"
	if toggle == true then
		for i, v in pairs(info.playerstable:GetChildren(math.random(1, #info.playerstable))) do
			if currentinsertteam == "Raiders" then
				--local currentplayer = v[math.random(1, #v)]
				v.Team = info.Raiders
				table.remove(info.playerstable, v)
				v = "Defenders"
			elseif currentinsertteam == "Defenders" then
				--local currentplayer = v[math.random(1, #v)]
				v.Team = info.Defenders
				table.remove(info.playerstable, v)
				v = "Raiders"
			end

		end
	elseif toggle == false then
		for i, v in pairs(Players:GetChildren()) do
			v.Team = nil
		end
	end
end
local tabl = info.playerstable
local randomplayer = tabl[math.random(1, #tabl)]

so did you mean this?

for i, v in pairs(info.playerstable:GetChildren(math.random(1, #info.playerstable))) do
	if currentinsertteam == "Raiders" then
	    local tabl = info.playerstable
        local randomplayer = tabl[math.random(1, #tabl)]
		randomplayer.Team = info.Raiders
		table.remove(info.playerstable, currentinsertteam )
		currentinsertteam = "Defenders"
	end
 end

yeah pretty much

insertabsolutelyrandomtexthere

I still got the same error :confused:
ServerScriptService.RoundSystem.script.Main:35: attempt to call missing method ‘Getchildren’ of table

oh yea i see the issue, replace line 35 with

for i, v in pairs(info.playerstable) do

It works! Thankyou for your help!

nah it dosen’t work when there’s 2 or more player :confused:
I think that the error come form the remove line but I don’t know how to fix

I made a simple script that shows you how to iterate through a list to pick a random instance, and then proceed to get the children of that single instance.

I assumed in your original code you wanted to get the children of the character, so I also made a search for the model of the character. I was unsure if you wanted to pick a random character or a random child of all characters, so I went for the first option since that seemed most logical. If I am wrong, please let me know and I will update the code accordingly.

local Players = game:GetService("Players")

local playerList = {}

game.Players.ChildAdded:Connect(function(player) --//when a player joins the game, add to the list
	table.insert(playerList, player)
end)

local function pickRandomPlayer()
	local pickedPlayer = playerList[math.random(1, #playerList)] --//pick a random player
	print(pickedPlayer, " is picked at random.")
	local findCharacter = game.Workspace:GetChildren() --//used to find player's model
	for i, v in findCharacter do --//iterate through the workspace to find a match with the picked player
		if v.Name == pickedPlayer.Name then --//if there is a match, then
			local childrenOfPlayer = v:GetChildren() --//get the children of the player's character
			print(childrenOfPlayer) --//print the table of children in the output console
		end
	end
end

task.wait(3)

pickRandomPlayer() --//activate the function

Note: I acknowledge that this code may not be applied directly in your script, since the end result or execution may differ in some ways. Yet this does show how to solve the question you asked in your topic. You can use the code I made as an example or template to mend it more closely to your liking.

1 Like

thx for your help, it works now

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