Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Is there a more efficient way to run this touch script that doesn't lag out the game?

Asked by 6 years ago

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?

0
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. Launderer 343 — 6y
0
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. Wolf5429 15 — 6y

1 answer

Log in to vote
0
Answered by 6 years ago

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.

0
I can try this, I'll accept the answer if it works, thanks. Wolf5429 15 — 6y
0
I've used this for a gui to pop up while they are on the part. DogeDark211 81 — 6y
0
It doesn't work for what I'm trying to do but I'll definitely use this technique somewhere else. Wolf5429 15 — 6y
Ad

Answer this question