Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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.

4 comments

0

Possibly add a debug variable?

0

This may seem like i'm dumb, but I am new to scripting. How would I add a debug variable?

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.

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.

2 answers

✓ Accepted answer

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

1 comment

0

It worked once and then it didn't work.

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.

1 comment

0

You're supposed to place the wait(1) debounce = true within the if statements. Not outside of the event.

This discussion is preserved as it was. New answers, comments, and votes are closed.