Function and return error

hi, i was trying to make a tool like f3x but when i want to detect what part is selected, but it don’t return the object

the script:


local function GetChildrens(parent, name)
	local i = parent:GetChildren()
	if #i > 0 then
		for _, v in next, i do
			if tostring(v.Name) == tostring(name) then
				return v
			else
				GetChildrens(v,tostring(name))
			end
		end
	end
end

Events.ChangeColor.OnServerEvent:Connect(function(plr,color)
	local g = GetChildrens(workspace,'BToolsSelectionBox '..plr.Name)
		   if g then
		   warn('a')
		   g.BrickColor = color
		end
	end)

1 Like

Currently your code is looking through all the children in workspace to try and find an object with the name BToolsSelectionBox pro_developer213 so unless there is an object called BToolsSelectionBox pro_developer213 it won’t return anything. To double check that this is the case, after the loop add another return and see what it is returning, the code should look like this:

local function GetChildrens(parent, name)
	local i = parent:GetChildren()
	if #i > 0 then
		for _, v in next, i do
			if tostring(v.Name) == tostring(name) then
				return v
			else
				GetChildrens(v,tostring(name))
			end
		end
        return 'Error'
	end
end

Events.ChangeColor.OnServerEvent:Connect(function(plr,color)
	local g = GetChildrens(workspace,'BToolsSelectionBox '..plr.Name)
    print(g)
	if g then
		warn('a')
		g.BrickColor = color
	end
end)

still don’t work and don’t print a, i don’t see any error and u can’t use print g becouse the function return a Instance not a Instance name

FindFirstChild’s second argument is a boolean stating if it’s “recursive”, this means that if it doesn’t find it in the given Name inside the Instance FindFirstChild is being called upon, it will search it’s decendants and keep going down hierarchies till it finds it.

Putting that into practice, you can define g like this:

local g = workspace:FindFirstChild("BToolsSelectionBox " .. plr.Name, true)
1 Like

General suggestions and solution to the original issue for the sake of completion, but consider not calling tostring on things you already know are strings, such as an instance’s .Name or a string parameter you’ve already passed.

i is an array so it’s also best to do in ipairs(i) as opposed to in next, i.

Additionally, the reason your original approach wasn’t working is because after the first layer of iteration, GetChildrens(v, tostring(name)) is called but its return value is never used, so when the right descendant is found it would never be propagated upwards.

GetChildren() gives a array not a table?

An array can be part of a table. When it comes to Lua, tables refer to associative arrays as per implementation. Tables are capable of having an array and a hash (dictionary) part. So in simpler terms, an array is (part of) a table and so is a dictionary. You could have both in one table and that would result in something called a mixed table.

local array = {"item1", "item2", "item3"}

local dictionary = {
    key1 = "value1",
    key2 = "value2",
    key3 = "value3"
}

On Roblox, we typically call an array a table that doesn’t utilise the hash part (otherwise setting a key and a value) and a dictionary a table that doesn’t use the array part. This is as demonstrated above.

GetChildren returns an array of objects in order I believe according to when they were added as a child of the parent object. Don’t be tricked by the Studio explorer; it only sorts them alphabetically for the sake of convenience.

More reading if you’re interested:
https://www.lua.org/pil/2.5.html

1 Like