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

Can someone tell me why this script makes my game very laggy? It lags the game for everyone.

Asked by 3 years ago

Hello. I have three forcefield doors and one of these scripts are in each of them. When I play the game and use the forcefields the game turns really laggy. I'm not really sure what the issue is, but it could just be an easy fix. Here is the script i'm using:

local door = script.Parent
local Republic = game.Teams.Republic
local RI = game.Teams.RI
local BlueTeam = game.Teams["501st"]
local CG = game.Teams["Coruscant Guard"]
local RG = game.Teams["Red Guard"]
local RC = game.Teams.Commandos

door.Touched:Connect(function(hit)
    local humanoid = hit.Parent:FindFirstChild("Humanoid")
    if humanoid then
        local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
        if plr.Team == Republic then
            door.CanCollide = false
            door.Transparency = .5
            wait(1)
            door.CanCollide = true
            door.Transparency = 0 
        elseif plr.Team == RI then
            door.CanCollide = false
            door.Transparency = .5
            wait(1)
            door.CanCollide = true
            door.Transparency = 0 
        elseif plr.Team == BlueTeam then
            door.CanCollide = false
            door.Transparency = .5
            wait(1)
            door.CanCollide = true
            door.Transparency = 0 
        elseif plr.Team == CG then
            door.CanCollide = false
            door.Transparency = .5
            wait(1)
            door.CanCollide = true
            door.Transparency = 0
        elseif plr.Team == RG then
            door.CanCollide = false
            door.Transparency = .5
            wait(1)
            door.CanCollide = true
            door.Transparency = 0
        elseif plr.Team == RC then
            door.CanCollide = false
            door.Transparency = .5
            wait(1)
            door.CanCollide = true
            door.Transparency = 0
        end
    end
end)

If you could help me fix this problem I would be very happy. Thank you.

0
Possibly add a debug variable? radiant_Light203 1166 — 3y
0
This may seem like i'm dumb, but I am new to scripting. How would I add a debug variable? peter21340 41 — 3y
0
Wait sorry I meant debounce. Basically add a local variable that stops the script from running more than once at a time which happens a lot to touched events. radiant_Light203 1166 — 3y
0
At line 08 lets say "local Debounce = false" and at the start of the event add "if Debounce == true then return end", the next line should turn Debounce to true and the last line of the function should have a small wait after which then turns Debounce back to false. radiant_Light203 1166 — 3y

2 answers

Log in to vote
1
Answered by 3 years ago

You'll need to add a variable to be used as a debounce. This is done by revising your script. After line 7, add this:

local debounce = false

and after the touched event, add:

if debounce == false then
     debounce = true
     --Rest of code in your function
end --This goes at the end of your touched event
wait(1)
debounce = false
0
It worked once and then it didn't work. peter21340 41 — 3y
Ad
Log in to vote
0
Answered by 3 years ago
Edited 3 years ago

I'm not really sure what I did wrong, but bare with me if its a stupid mistake. Here is the changed code:

local door = script.Parent
local Republic = game.Teams.Republic
local RI = game.Teams.RI
local BlueTeam = game.Teams["501st"]
local CG = game.Teams["Coruscant Guard"]
local RG = game.Teams["Red Guard"]
local RC = game.Teams.Commandos
local debounce = false

door.Touched:Connect(function(hit)
    if debounce == false then
        debounce = true
        local humanoid = hit.Parent:FindFirstChild("Humanoid")
        if humanoid then
            local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
            if plr.Team == Republic then
                door.CanCollide = false
                door.Transparency = .5
                wait(1)
                door.CanCollide = true
                door.Transparency = 0 
            elseif plr.Team == RI then
                door.CanCollide = false
                door.Transparency = .5
                wait(1)
                door.CanCollide = true
                door.Transparency = 0 
            elseif plr.Team == BlueTeam then
                door.CanCollide = false
                door.Transparency = .5
                wait(1)
                door.CanCollide = true
                door.Transparency = 0 
            elseif plr.Team == CG then
                door.CanCollide = false
                door.Transparency = .5
                wait(1)
                door.CanCollide = true
                door.Transparency = 0
            elseif plr.Team == RG then
                door.CanCollide = false
                door.Transparency = .5
                wait(1)
                door.CanCollide = true
                door.Transparency = 0
            elseif plr.Team == RC then
                door.CanCollide = false
                door.Transparency = .5
                wait(1)
                door.CanCollide = true
                door.Transparency = 0
            end
        end
    end
end)
wait(1)
debounce = false

I also tried to put the wait part inside the script, but still won't work.

0
You're supposed to place the wait(1) debounce = true within the if statements. Not outside of the event. Fifkee 2017 — 3y

Answer this question