Extreme Script Activity Using :GetPartBoundsInBox (Up to 30%)

Recently I decided to make a music system using the new :GetPartBoundsInBox() function. At first glance it worked extremely well, but then I noticed that as I extended that box to include an increasing number of parts, the script activity also increased dramatically. That’s despite the fact that I used OverlapParams, too. (If I print out the results of the :GetPartBoundsInBox() function it does only print out what I specified)

This is extremely worrying, as I care very much about performance in my games so that anybody can play them without experiencing intense lag. Is there any way I can cut down on the activity by inserting new code/further optimizing the code I currently have? (I’ve also found that shrinking and splitting the region up as to not include as many parts inside works, but I’m wondering if there’s anything that I can do besides that)

local rs = game:GetService("RunService")

local ovp = OverlapParams.new()
ovp.FilterType = Enum.RaycastFilterType.Whitelist
ovp.FilterDescendantsInstances = {game.Players.LocalPlayer.Character}

local music = workspace:WaitForChild("Music")
local regionboxes = workspace.RegionBoxes

local function regioncheck()
	for r,f in pairs(workspace.RegionBoxes:GetChildren()) do -- f is the regionbox
		local regionw = workspace:GetPartBoundsInBox(f.CFrame,f.Size,ovp) -- grabbing the dimensions of the region box and actually getting the parts inside
		if table.find(regionw, game.Players.LocalPlayer.Character.HumanoidRootPart) then 
			if not music:FindFirstChild(f.Value.Value).Playing then -- f.Value.Value is a StringValue that contains the name of the music that's supposed to be playing, it then searches the folder for that exact name 
					for e,d in pairs(music:GetChildren()) do -- Stops all music here to make sure that there isn't any music overlapping
						d:Stop()
					end
				music:FindFirstChild(f.Value.Value):Play()
				if f:FindFirstChild("ZN") then -- This is just some extra gui stuff to have the title of the zone pop up 
					if game.Players.LocalPlayer.PlayerGui:FindFirstChild("ZoneName") then game.Players.LocalPlayer.PlayerGui:FindFirstChild("ZoneName"):Destroy() end
					local zg = game.ReplicatedStorage.Guis.ZoneName:Clone()
					zg.Frame.TextLabel.Text = f:FindFirstChild("ZN").Value
					zg.Parent = game.Players.LocalPlayer.PlayerGui
				end
			end
		end
	end
end

rs.Heartbeat:Connect(regioncheck)

Also, for a little further context, this is a local script located in StarterCharacterScripts.

1 Like

You don’t need to check every frame. Try every second or two times per second, it should be plenty fast.

Instead of searching through regions until you fine one that the player is in, you could just check which region the player is overlapping. For example

local regionOverlapParams = OverlapParams.new()
regionOverlapParams.FilterType = Enum.RaycastFilterType.Whitelist
regionOverlapParams.FilterDescendantInstances = CollectionService:GetTagged("RegionMarker")

local currentRegion

function updateCurrentRegion()
    local previousRegion = currentRegion
    local touchingRegions = workspace:GetPartsInPart(humanoidRootPart, regionOverlapParams)
    
    currentRegion = touchingRegions[1]
    if #touchingRegions > 1 then
        warn(("Player is touching %g regions at the same time (%s)!"):format(#touchingRegions, table.concat(touchingRegions, ", "))) 
    end
   
    if previousRegion ~= currentRegion then
        onRegionExited(currentRegion)
        onRegionEntered(currentRegion)
    end
end

function onRegionEntered(region)
end

function onRegionExited(region)
end

while true do
    updateCurrentRegion()
    wait(REGION_CHECK_DELAY)
end
3 Likes

To use the :FindPartOnRayWithWhitelist() function in Roblox to check if the player’s character is intersecting a region box, you can use the following steps:

  1. Create an OverlapParams object and set its FilterType property to Enum.RaycastFilterType.Whitelist. This will ensure that only the parts specified in the FilterDescendantsInstances property will be considered when performing the raycast.
  2. Set the FilterDescendantsInstances property of the OverlapParams object to an array containing the player’s character. This will tell the function to only consider the player’s character when performing the raycast.
  3. Use the :FindPartOnRayWithWhitelist() function to check if the player’s character is intersecting the region box. To do this, you can use the :FindPartOnRayWithWhitelist() function and pass in the CFrame and size of the region box, as well as the OverlapParams object you created in step 1.
  4. If the :FindPartOnRayWithWhitelist() function returns a part, it means the player’s character is intersecting the region box. You can then play the appropriate music and update the GUI as needed.

Here’s an example of how the modified code might look:

local ovp = OverlapParams.new()
ovp.FilterType = Enum.RaycastFilterType.Whitelist
ovp.FilterDescendantsInstances = {game.Players.LocalPlayer.Character}

local function regioncheck()
	for r,f in pairs(workspace.RegionBoxes:GetChildren()) do
		local hit, pos, norm = workspace:FindPartOnRayWithWhitelist(f.CFrame, f.Size, ovp)
		if hit then
			if not music:FindFirstChild(f.Value.Value).Playing then
				for e,d in pairs(music:GetChildren()) do
					d:Stop()
				end
				music:FindFirstChild(f.Value.Value):Play()
				if f:FindFirstChild("ZN") then
					if game.Players.LocalPlayer.PlayerGui:FindFirstChild("ZoneName") then game.Players.LocalPlayer.PlayerGui:FindFirstChild("ZoneName"):Destroy() end
					local zg = game.ReplicatedStorage.Guis.ZoneName:Clone()
					zg.Frame.TextLabel.Text = f:FindFirstChild("ZN").Value
					zg.Parent = game.Players.LocalPlayer.PlayerGui
				end
			end
		end
	end
end

I hope this helps! Let me know if you have any other questions.

1 Like

Could’ve hidden ChatGPT there mate.

2 Likes

the code ain’t mine, so I’m not taking credit, but I wanted to help people with AI
and sometimes it can be wrong, but this AI will learn from your mistakes, and so that’s why I’m loving it.

And I know some people won’t like while doing, but I had helped a lot of people this

so people can hate me, but I’m not breaking any rule

the only one that might be breaking is off-topic because I’m trying to respond back to people

"I believe in the value of compassion and the importance of supporting others. That’s why I’m always happy to help out

1 Like

The AI being wrong is not the point. The point is literally sending a whole wrong answer that can be seen at first glance while you’re not even hiding the fact you’re using an AI.

If you want to post AI code, which in nowhere says it’s not allowed, then do it properly. Mistakes happen but this one is way too clear.

4 Likes

I appreciate your thoughts and insights. Thank you for your feedback.

Holy moly, thanks! I didn’t even know that CollectionService was a thing, seems like it’ll be incredibly useful to look into some more. A little embarrassed that I didn’t think about decreasing how often I checked the regions, haha

1 Like

Hi, just a suggestion but maybe you could preface any AI answers by saying that it’s AI generated, or even just telling people how they can use AI to get those answers themselves. Passing off AI generated content as our own is how we get AI content banned, and I don’t want to see that happen either.

Sorry for bumping an off-topic comment, I’m replying because your PMs are off.

2 Likes

Stop using ChatGPT to respond to answers

4 Likes

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