Game lags whenever a tagged part is stepped on

I’m currently working on a kill part system that uses collection services, however, the only issue is that it lags whenever I step on a tagged part. I just wanna know of any potential ways to optimize it.
code:


-- Services --
local Collection_Service = game:GetService("CollectionService")

-- Core --
local Core = script.Parent:WaitForChild("Core")
local GR_Point = Core:WaitForChild("Respawn_Point")

function Reset(Humanoid_Root_Part, Respawn_Point)
	Humanoid_Root_Part.CFrame = Respawn_Point.CFrame
end


while true do
	task.wait(0.02)
	for I,V in pairs(Collection_Service:GetTagged("Kill_Part")) do
		V.Touched:Connect(function(Hit)
			print(GR_Point.Value.CFrame)
			if Hit.Parent:FindFirstChild("Humanoid") then
				local Humanoid_Root_Part = Hit.Parent:FindFirstChild("HumanoidRootPart")
				if Humanoid_Root_Part then
					Reset(Humanoid_Root_Part, GR_Point.Value)
				end
			end
		end)
	end
end
3 Likes

First of all, let’s avoid using while true do loops since they can be avoided with simple events. You are calling that event every 0.02 seconds, why? Events must run once. If you just create more then you will be using more memory, that is why your game lags when you step on it, because there are so many registered events at once that your script just overloads and starts lagging. Remove while true do and you will be completely fine.

local CollectionService = game:GetService("CollectionService")
local Core = script.Parent:WaitForChild("Core")
local GR_Point = Core:WaitForChild("Respawn_Point")

function Reset(Humanoid_Root_Part, Respawn_Point)
	Humanoid_Root_Part.CFrame = Respawn_Point.CFrame
end

local killParts = CollectionService:GetTagged("Kill_Part")

for _, v in killParts do
	v.Touched:Connect(function(Hit)
		print(GR_Point.Value.CFrame)
		if Hit.Parent:FindFirstChild("Humanoid") then
			Reset(Hit.Parent:FindFirstChild("HumanoidRootPart"), GR_Point.Value)
		end
	end)
end

Also please avoid nesting since it’s bad. Here is some video material for you to watch to avoid making these mistakes in the future:
How to correctly name stuff in code, you had some of those mistakes as well
Premature Opitimization
Why you shouldn’t nest your code

Chat GPT | 4 Answer

From your code, it’s clear that you are using the CollectionService to tag parts in your game, and then using an infinite loop to continuously check for a Touched event on these parts. This approach is quite inefficient and might be causing the lag you’re experiencing.

Here’s why:

  • The GetTagged method is called every 0.02 seconds. This method can be quite expensive, especially if you have many parts in your game. It’s best to call this method only once or when the tags change (Source 1).
  • You are connecting a new Touched event every 0.02 seconds. This means that after 1 second, you will have 50 Touched events connected to each part. This can lead to a massive number of events being connected over time, which can significantly slow down your game.

Here’s how you can optimize your code:

  1. Call GetTagged only once or when the tags change:

    local killParts = Collection_Service:GetTagged("Kill_Part")
    

    This line of code should be outside of the loop because you only need to get the tagged parts once.

  2. Connect the Touched event outside of the loop and only once per part:

    for I, V in pairs(killParts) do
        V.Touched:Connect(function(Hit)
            -- your code here
        end)
    end
    

    This code also should be outside the loop. It connects the Touched event to each part only once.

  3. Remove the infinite loop:

    The infinite loop is unnecessary because the Touched event will automatically be fired whenever a part is touched.

Here’s the optimized version of your code:

-- Services --
local Collection_Service = game:GetService("CollectionService")

-- Core --
local Core = script.Parent:WaitForChild("Core")
local GR_Point = Core:WaitForChild("Respawn_Point")

function Reset(Humanoid_Root_Part, Respawn_Point)
    Humanoid_Root_Part.CFrame = Respawn_Point.CFrame
end

local killParts = Collection_Service:GetTagged("Kill_Part")

for I, V in pairs(killParts) do
    V.Touched:Connect(function(Hit)
        print(GR_Point.Value.CFrame)
        if Hit.Parent:FindFirstChild("Humanoid") then
            local Humanoid_Root_Part = Hit.Parent:FindFirstChild("HumanoidRootPart")
            if Humanoid_Root_Part then
                Reset(Humanoid_Root_Part, GR_Point.Value)
            end
        end
    end)
end

This code will perform much better because it avoids unnecessary function calls and event connections.

Use bots like Chat GPT to your advantage, he might not always be right, but in most cases he can help you figure out why. I straight up pasted your question and code and he figured out why easily.
Do NOT copy and paste code from chat-gpt, instead take that code as an example and use it to build your code faster and easier. Those bots may not always be accurate, but in cases like these you should always give it a try first before asking a friend or on some forum.
phind - free chat-gpt 4 if you need it
full summary

5 Likes

Now whenever I step on it, it just doesn’t work at all

1 Like

so throw it into a renderstepped connected function using the runtime service

2 Likes

You could just replace the while true loop with render stepped that way this issue doesnt happen

2 Likes

isn’t renderstepped client based?

2 Likes

yes. I haven’t used collection service in a while so i have forgotten if it is client or not

2 Likes

Collection service is server based, which is why I tried using a while loop at first. Even if I integrated renderstepped, I’d still need to fire an event, which would make it even more laggy, I’m genuinely stumpped

1 Like

that is an weird issue then… Are you handling alot of stuff on the client/server? Have you tried waiting before spamming the server with remote calls? Also when making a while true loop do this instead: while task.wait() do it is the same just has a built in wait.

Oh, it’s all been resolved. Turns out, I needed to use a function to get it to work.

The solution to this problem can be found here: Trouble using collection service to make killparts