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