Script Not Functioning Properly When Region3 Radius Increase

So what I’m trying to achieve is that the player can see all information about the models that has humanoid in a radius. (It is heavily inspired by an anime named “Jujutsu Kaisen,” the character “Gojo Satoru.”) But the issue is that when I increase the radius, the script is very delayed.

It’s probably Roblox right? Like the code it self has no problem right?

If anyone knows the problem about this code or an alternative way please let me know.

Radius set to 10

Radius Set to 20

Radius set to 30

You can tell as I increase the radius, the more dummies/NPCs are not being detected all at once.

The script:- (This is a local script which means it is client sided.)

local player = game.Players.LocalPlayer
local radius = 20

local function checkRegion()
	local character = player.Character
	if not character then
		character = player.CharacterAdded:Wait()
	end

	local region = Region3.new(character.HumanoidRootPart.Position - Vector3.new(radius, radius, radius), character.HumanoidRootPart.Position + Vector3.new(radius, radius, radius))
	local partsInRegion = workspace:FindPartsInRegion3(region, nil)

	for _, object in ipairs(partsInRegion) do
		if object.Parent and object.Parent:FindFirstChildOfClass("Humanoid") then
			local distance = (object.Position - character.HumanoidRootPart.Position).Magnitude
			if distance <= radius then
				if object.Parent.Name ~= player.Character.Name then
					if not object.Parent:FindFirstChild("SixEyesHighlight") then
						local highlight = Instance.new("Highlight", object.Parent)
						highlight.FillTransparency = 1
						highlight.OutlineTransparency = 0
						highlight.OutlineColor = Color3.fromRGB(0, 255, 255)
						highlight.Name = "SixEyesHighlight"
						game.Debris:AddItem(highlight, 5)
						
						local effect1 = game.ReplicatedStorage.GojoSatoruRemote.Aura:Clone()
						effect1.Parent = object.Parent.HumanoidRootPart
						effect1.Enabled = true
						game.Debris:AddItem(effect1,5)
						

						local effect2 = game.ReplicatedStorage.GojoSatoruRemote.CoreAura:Clone()
						effect2.Parent = object.Parent.HumanoidRootPart
						effect2.Enabled = true
						game.Debris:AddItem(effect2,5)
						
						
						local name = game.ReplicatedStorage.GojoSatoruRemote.NameUI:Clone()
						name.Parent = object.Parent.HumanoidRootPart
						name.Enabled = true
						name.UI.Text = object.Parent.Name
						game.Debris:AddItem(name,5)
					end
				end
			end
		end
	end
end

game:GetService("RunService").Heartbeat:Connect(function()
	checkRegion()
end)

With the help of ChatGPT, I was able to use another way of detecting instead of Region3, I used magnitude which made the script function just how I wanted.

local player = game.Players.LocalPlayer
local radius = 50

local function checkMagnitude()
	local character = player.Character
	if not character then
		character = player.CharacterAdded:Wait()
	end

	local characterPosition = character.HumanoidRootPart.Position

	for _, object in ipairs(workspace:GetDescendants()) do
		if object:IsA("BasePart") and object.Parent and object.Parent:FindFirstChildOfClass("Humanoid") then
			local partPosition = object.Position
			local distance = (partPosition - characterPosition).Magnitude

			if distance <= radius then
				if object.Parent.Name ~= player.Character.Name then
					if not object.Parent:FindFirstChild("SixEyesHighlight") then
						local highlight = Instance.new("Highlight", object.Parent)
						highlight.FillTransparency = 1
						highlight.OutlineTransparency = 0
						highlight.OutlineColor = Color3.fromRGB(0, 255, 255)
						highlight.Name = "SixEyesHighlight"
						game.Debris:AddItem(highlight, 5)

						local effect1 = game.ReplicatedStorage.GojoSatoruRemote.Aura:Clone()
						effect1.Parent = object.Parent.HumanoidRootPart
						effect1.Enabled = true
						game.Debris:AddItem(effect1, 5)

						local effect2 = game.ReplicatedStorage.GojoSatoruRemote.CoreAura:Clone()
						effect2.Parent = object.Parent.HumanoidRootPart
						effect2.Enabled = true
						game.Debris:AddItem(effect2, 5)

						local name = game.ReplicatedStorage.GojoSatoruRemote.NameUI:Clone()
						name.Parent = object.Parent.HumanoidRootPart
						name.Enabled = true
						name.UI.Text = object.Parent.Name
						game.Debris:AddItem(name, 5)
					end
				end
			end
		end
	end
end

game:GetService("RunService").Heartbeat:Connect(function()
	checkMagnitude()
end)

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