Selecting every second studt

Hello developers.

I want to make it, so that every Second studt here in this demonstration gets selected clockwise. How would I do that? Using ipairs won’t achieve it.

image

I haven’t really tried as I do not know how to do that with the most optimization possible as the finished result will be a map that has to be loaded in so yeah.

Thanks to everyone who is able to help me with this.

You want something like this?

Screenshot 2024-05-19 182850

Basically, yeah. I just want to make a gauntlet where every 2 seconds every second part disappears.

Here is it:

local folder = script.Parent.TestParts -- folder
local children = folder:GetChildren() -- children

local function calculateAngle(center, part) -- function to calculate the angle between the part and the center
	local direction = (part.Position - center).Unit
	local angle = math.atan2(direction.Z, direction.X)
	return angle
end

local center = Vector3.new(0, 0, 0) -- center position

for _, part in ipairs(children) do
	center = center + part.Position
end
center = center / #children

print("Center position:", center)

-- Sort parts based on their angle from the center
table.sort(children, function(a, b)
	return calculateAngle(center, a) < calculateAngle(center, b)
end)

-- Print the sorted parts for debugging
for i, part in ipairs(children) do
	print(i, part.Name, "Angle:", calculateAngle(center, part))
end

-- Function to hide every second part
local function hideEverySecondPart()
	for i = 2, #children, 2 do
		wait(2)
		children[i].Transparency = 1
		children[i].CanCollide = false -- or destoy whatever u want
	end
end


-- Main loop to hide parts every 2 seconds
while true do
	hideEverySecondPart()
end

Screenshots:

Screenshot 2024-05-19 183419

(Parts will start dissapearing)

Screenshot 2024-05-19 183428

1 Like

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