Trying to check multiple tag at once

I’m making a custom camera script and to prevent camera warping around while player is in combat
I make the script to filtering out part with certain Tags

now for the problem
is there a way to check multiply condition at once in a single loop? because rn I run 3 for loops in a single script which I feel like it could affect the game performance

-- Services
local PlayerService = game:GetService("Players")
local RunService = game:GetService("RunService")
local CS = game:GetService("CollectionService")

-- Variables
local Player = PlayerService.LocalPlayer
local CurrentCamera = workspace.CurrentCamera
local Angle = -50

-- new focus point
local focusPoint = Instance.new("Part")
focusPoint.Anchored = false
focusPoint.Parent = Player.Character:FindFirstChild("Torso")
focusPoint.CanCollide = false
focusPoint.CanQuery = true
focusPoint.CanTouch =false
focusPoint.Size = Vector3.new(1,1,1)
focusPoint.Transparency = 1

-- create a mortor6d and connect focuspoint to the players torso
local motor6d = Instance.new("Motor6D")
motor6d.Parent = focusPoint
motor6d.C0 = CFrame.new(0, 3.5, 0)
motor6d.Part0 = focusPoint
motor6d.Part1 = Player.Character.Torso

-- Ignored prop part
local Prop = CS:GetTagged("Prop")
local Item = CS:GetTagged("Item")
local Enemy = CS:GetTagged("Enemy")

-- Cause Camera Lag
local lagFactor = 0.04 -- adjust lag (1=No Lag, 0 = Broken, 0.01 = Lot of lag)

local lastPosition = Vector3.new()
local lastRotation = CFrame.new()

CurrentCamera.CameraType = Enum.CameraType.Scriptable

RunService.RenderStepped:Connect(function(deltaTime)
	if Player.Character then
		local humRootPart = focusPoint
		if humRootPart then

			-- lock the camera and make it follow player
			local targetPosition = humRootPart.Position + Vector3.new(0, 20, 15)
			local targetRotation = CFrame.Angles(math.rad(Angle), 0, 0)

			-- apply cam lag
			lastPosition = lastPosition:Lerp(targetPosition, lagFactor)
			lastRotation = lastRotation:Lerp(targetRotation, lagFactor)


			CurrentCamera.CFrame = CFrame.new(lastPosition) * lastRotation
			
			
			-- cam collision to prevent the cam from clipping through the wall
			local cameraRay = Ray.new(Player.Character.HumanoidRootPart.Position, CurrentCamera.CFrame.Position - Player.Character.HumanoidRootPart.Position)
			local Ignore = {Player.Character}
			
			-- check if part has the tag if yes, don't collide the cam with the part
			for i, Object in pairs(Prop) do
				if Object:HasTag("Prop") then
					table.insert(Ignore, Object)
				end
			end
			
			for i, Object in pairs(Item) do
				if Object:HasTag("Item") then
					table.insert(Ignore, Object)
				end
			end
			
			for i, Object in pairs(Enemy) do
				if Object:HasTag("Enemy") then
					table.insert(Ignore, Object)
				end
			end

			local HitPart, HitPosition = game.Workspace:FindPartOnRayWithIgnoreList(cameraRay, Ignore)

			CurrentCamera.CFrame = (CurrentCamera.CFrame - (CurrentCamera.CFrame.Position - HitPosition)) + (Player.Character.HumanoidRootPart.Position - CurrentCamera.CFrame.Position).Unit 
		end
	end
end)

Your code shouldn’t lag that much but:

Due to assigning the tagged objects only once it could miss new ones and not remove old ones

To fix that change each loop to this: (essentially just remove the variable)

for _, Object in CS:GetTagged("Prop") do
	table.insert(Ignore, Object)
end

But if you really don’t want to have loops then you can make the ignore list outside of the loop and just update it with game:GetService("CollectionService"):GetInstanceAddedSignal(tag):Connect()

1 Like

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