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

How can I track the Target?

When a the server is started, lets say there's a Part in Workspace and it gets RandomPart(1-3) (picks a random name for it) The RandomPart has a Target that i'm trying to get. But it's random and the targets for each RandomPart are the same. I want to get the target for any RandomPart. It could be any. All I need is that one Target. How would I do this? I tried this but it doesn't work.

Workspace > Part > RandomPart1 > Target

Workspace > Part > RandomPart2 > Target

Workspace > Part > RandomPart3 > Target

for i,v in pairs(game.Workspace.Part:GetChildren()) do
	Targetfound = v:FindFirstChild('Target')
end

Targetfound.BrickColor = BrickColor.new('Really red')

1 answer

✓ Accepted answer

-- So it's actually random
math.randomseed(tick()%1*1e6)
-- Use WaitForChild() on children
local folder = workspace:WaitForChild("Part")

local function find_target()
	-- Choose random part in the folder
	local all = folder:GetChildren()
	local rand = all[math.random(#all)]
	local target = rand:findFirstChild("Target")
	if target then
		target.BrickColor = BrickColor.new("Really red")
		return target
	end
	return false
end

local target = find_target()
print(target and 'Found the target' or "Target not found")

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