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?
Hello. Here is my idea:
You would have the part with a normal Touched function and when the player touches that part they get the effect. Add walls around it that are invisible and set can collide to false and add another touch function in it that removes the effect when the player touches it.