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:
-
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.
-
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.
-
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