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

Can I not loop through a folder of parts and give them a Region3?

I'm trying to give multiple parts a visible region3 where when you enter it a gui is enabled. The gui's timer goes up to sixty 60 seconds, gives money, then resets back to zero. It repeats this process as long as you're still in the region. When you leave it, the gui is disabled.

When assigning just ONE specific part a region3,the script works just fine. However, once I added the for loop in order to assign more than one part a region3, none of the parts work. The gui does not show up at all for any of them.

Is starterGui not compatible with more than one region3? Or is the way I'm giving the parts a region3 with the issue?

screenshot of the folder: https://imgur.com/a/Br8MyBN

local gui = script.Parent
local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()



	
for _, part in pairs(game.Workspace.Jobs.OfficeJob:GetChildren()) do

local Region = Region3.new(part.Position - (part.Size/2),part.Position + (part.Size/2))

while wait() do --keeps going as soon as you run the game
	--second parameter means workspace will keep detecting if any of the local player's character parts are in region, no need to check again cause it's a whitelist
	local Detect = workspace:FindPartsInRegion3WithWhiteList(Region,Character:GetDescendants(),math.huge)

	local timer = gui.Frame.Timer
	local seconds = timer.seconds
	local cash = Player.Stats.pocketMoney
	local TotalAmountLabel = gui.Frame.TotalAmount
	local amount = TotalAmountLabel.amount 

	if #Detect > 0 then
		gui.Enabled = true
		timer.Text = seconds.Value
		TotalAmountLabel.Text = "$" .. amount.Value
		wait(1)
		seconds.Value = seconds.Value - 1
		if seconds.Value == 0 then
			cash.Value = cash.Value + 500
			amount.Value = amount.Value + 500
			seconds.Value = 5
		end	

	else if #Detect == 0 then
			gui.Enabled = false	
			seconds.Value = 5
			amount.Value = 0
			timer.Text = seconds.Value
			TotalAmountLabel.Text = amount.Value
		end
	end
	end
	end

1 answer

Your current code is only making one region because of the infinite while loop in the for loop which won't proceed with the iteration until the loop ends, which'll probably be never.

Consider making a table which'll hold references to the Region3's you will create in a for loop and then iterating over the table and checking if the player is in any regions and do whatever in a while loop:

local Regions = {}
local isPlayerInARegion = false

for _, part in ipairs(game.Workspace.Jobs.OfficeJob:GetChildren()) do
    Regions[#Regions + 1] = Region3.new(part.Position - part.Size / 2, part.Position + part.Size / 2)
end

while true do
	for _, region in ipairs(Regions) do
		local Detect = workspace:FindPartsInRegion3WithWhiteList(Region, Character:GetDescendants(), math.huge)

		if #Detect > 0 and not isPlayerInARegion then
			isPlayerInARegion = true
			-- etc.
		elseif #Detect == 0 then
			isPlayerInARegion = false
			-- etc.
		end
	end
	wait()
end

The isPlayerInARegion variable is intended to make it not repeat multiple times if the player happens to be in overlapping Region3s

1 comment

0

It still doesn't work. I used print() statements to test if it even reaches if conditions.

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