Trying to get 1 child from :GetChildren()

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

  1. What do you want to achieve? I’m trying to get 1 child from :GetChildren()

  2. What is the issue? I can’t figure out how i can get 1 since everytime i try it gets multiple

  3. What solutions have you tried so far? I’ve looked almost everywhere and i can’t seem to find a fix.

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

local enabled = true
local checking = false

while task.wait(math.random(5,14)) do
	if math.random(1,10) ~= 10 then
		for _, v in pairs(script.Parent:GetChildren()) do
			if checking == false then
				if v.Name ~= "GoldenSlapple" and v.Name ~= "Ground" then
				checking = true
				enabled = true
				local glove = v
				v.Glove.Transparency = 0
				v.HandleLight.Transparency = 0
				v.Glove.PointLight.Enabled = true

				for _, a in pairs(v:GetChildren()) do
					if a:IsA("Texture") then
						a.Transparency = .1
					end
				end
				checking = false

				v.Glove.Touched:Connect(function(hit)
					if hit.Parent:FindFirstChild("Humanoid") and enabled == true then
						game:GetService("Players"):FindFirstChild(hit.Parent.Name).leaderstats.Slaps.Value += math.random(1,10)
						enabled = false
						v.Glove.Transparency = 1
						v.HandleLight.Transparency = 1
						v.Glove.PointLight.Enabled = false
						v.Sound:Play()

						for _, a in pairs(v:GetChildren()) do
							if a:IsA("Texture") then
								a.Transparency = 1 
							end
						end
					end

				end)

			end
			end
			end
			
	else
		for _, v in pairs(script.Parent:GetChildren()) do
			if v.Name ~= "Slapple" and v.Name ~= "Ground" then 
				enabled = true
				local glove = v
				v.Glove.Transparency = 0
				v.HandleLight.Transparency = 0
				v.Glove.PointLight.Enabled = true
				v.Attachment.Flare.Enabled = true

				for _, a in pairs(v:GetChildren()) do
					if a:IsA("Texture") then
						a.Transparency = .1
					end
				end

				v.Glove.Touched:Connect(function(hit)
					if hit.Parent:FindFirstChild("Humanoid") and enabled == true then
						game:GetService("Players"):FindFirstChild(hit.Parent.Name).leaderstats.Slaps.Value += math.random(50,100)
						enabled = false
						v.Glove.Transparency = 1
						v.HandleLight.Transparency = 1
						v.Glove.PointLight.Enabled = false
						v.Attachment.Flare.Enabled = false
						v.Sound:Play()

						for _, a in pairs(v:GetChildren()) do
							if a:IsA("Texture") then
								a.Transparency = 1 
							end
						end
					end

				end)

			end
		end
	end
end

Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.

1 Like

:GetChildren() returns a table. You can index an item by a number in square brackets.
Like this:

local anything = idontcare:GetChildren()[1]
2 Likes

You Can use findfirstchild(“child.name”)

1 Like

To get a single child you could:

--if you know the name
local child = target:FindFirstChild("nameofchild")

--if you know the specific type
local child = target:FindFirstChildWhichIsA("texture")

--if you just want the first thing it finds
local child = target:GetChildren()[1]

--loop with a break 
local child
for i, v in target:GetChildren() do
    if v:IsA("texture") then
        child = v
        break
    end
 end
if child then
    --do something 
else
    print("no textures found")
end  
1 Like

If you want just a random one, do local List = script.Parent:GetChildren() local ChosenObject = List[math.random(1,#List)]

2 Likes

GetChildren()[math.random(1, 10)]

1 Like

omg i forgot about using that anyway thank you for helping and reminding me :slight_smile:

1 Like

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