Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Giving to all children?

How would I give a certain object to all of something's children? For example I have an intvalue which I need to give to two children of a model. I know that I could put it in a loop that goes twice to clone it, make sure that the randomly selected one doesnt have it, give it , and give it to the other one if it does have it. This seems inefficient and i was wondering if that was a much faster way that works with more then two children too.

1 answer

✓ Accepted answer

An easy and efficient way to do what you want to do, as well as ensure that no duplicate math.random's occur, is to just remove the table entry when it's selected since the table.remove function returns the value that is removed(shown on line 7);


local intvalue = script.Parent.IntValue --Your intvalue
local model = script.Parent.Model --Your model

function giveTo(noc) --Parameter noc = Number of Children
	local children = model:GetChildren();
	for i = 1,c do --For every noc given
		local child = table.remove(children,i);
		--ensure it doesn't already own it
		if not child:FindFirstChild("IntValue") then
			--if it doesn't, give it one
			intvalue:Clone().Parent = child
		end
	end
end

--Usage;
giveTo(2);
 --This gives 2 children of 'model' 1 'intvalue' if they don't own it

1 comment

0

doesn't work

This discussion is preserved as it was. New answers, comments, and votes are closed.