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