This script is a while true loop that has a touch event inside it to activate an effect. I have put the touch event inside a while true loop because if a player was inside the part when an effect went away, they wouldn't receive another effect because the part wouldn't sense the player inside the part. The problem is that this loop lags out the entire game because it's calling so many remote events over and over again. So is there any more efficient way to run this script?
This code is inside a normal script inside the part:
local Damage = script.Parent
local player = game.Players:WaitForChild("Player1")
local ten = true
while true do
wait()
Damage.Touched:Connect(function(hit)
if not ten then return end
ten = false
wait()
local ehum = hit.Parent:FindFirstChild("Humanoid") or hit.Parent.Parent:FindFirstChild("Humanoid")
if ehum and ehum ~= player.Character.Humanoid then
local chr = ehum.Parent
local Player = game.Players:GetPlayerFromCharacter(chr)
game.ReplicatedStorage.Distortion:FireClient(Player)
end
ten = true
end)
end
And this code is inside a localscript inside starterplayerscripts:
game.ReplicatedStorage.Distortion.OnClientEvent:Connect(function(Player)
if game.Lighting:FindFirstChild("Distort") ~= nil then
wait()
elseif game.Lighting:FindFirstChild("Distort") == nil then
local Distort = game.ReplicatedStorage.AttackThings.Effects.Distort:Clone()
Distort.Parent = game.Lighting
wait(30)
Distort:Destroy()
end
end)
Any help please?

2 comments
i dont believe you need the while true do because the touched event fires only when it gets touched so i don't believe the while true do is needed.
That's exactly what I thought, but the problem is that the part doesn't sense players within the part. I was googling for a "whiletouching" kind of event and couldn't find anything. The part will not pick up players inside the part unless I have that loop there which is the problem because the loop lags the game out.