Why does this keep glitching out?

Hello! I am trying to make an gate open and close with a click of a GUI button. I sorta have it working, it opens but then closes immediately after.

Example of what I mean: https://gyazo.com/156f24de251a7d61db1f17252a836184

Code:

LocalScript GUI Side -

GateBtn.MouseButton1Click:Connect(function()
	if GateOpen == false then
		GateOpen = true
		OpenLaneGate:FireServer()
		GateBtn.TextButton_Roundify_12px.TextLabel.Text = "Close Gate"
		GateBtn.TextButton_Roundify_12px.ImageColor3 = Color3.new(0, 1, 0)
	elseif GateOpen == true then
		GateOpen = false
		GateBtn.TextButton_Roundify_12px.TextLabel.Text = "Open Gate"
		GateBtn.TextButton_Roundify_12px.ImageColor3 = Color3.new(1, 0, 0)		
		CloseLaneGate:FireServer()
	end
end)

Server Side -

local OpenLaneGateEvent = game.ReplicatedStorage.OpenLaneGate
local CloseLaneGateEvent = game.ReplicatedStorage.CloseLaneGate
 
local hingeGroup = game.Workspace:WaitForChild("BoothMainEntrance"):WaitForChild("HingeGroup1")
local primCF = hingeGroup:GetPrimaryPartCFrame()
 
 
local Cooldown = false
 
 
local function OpenGateFired()
	if Cooldown == false then
		Cooldown = true
		for i = 0, 1, 0.05 do
			hingeGroup:SetPrimaryPartCFrame(primCF:Lerp(primCF * CFrame.Angles(0, 0, math.rad(-75)), i))
			wait()
		end
 
		hingeGroup:SetPrimaryPartCFrame(primCF * CFrame.Angles(0, 0, math.rad(-75)))
		wait(10)
		Cooldown = false
		if Cooldown == true then
 
		end
	end
end
 
 
local function CloseGateFired()
	for i = 1, 0, -0.05 do
		hingeGroup:SetPrimaryPartCFrame(primCF:Lerp(primCF * CFrame.Angles(0, 0, math.rad(-75)), i))
		wait()
	end
 
	hingeGroup:SetPrimaryPartCFrame(primCF)
end
 
 
 
OpenLaneGateEvent.OnServerEvent:Connect(CloseGateFired)
OpenLaneGateEvent.OnServerEvent:Connect(OpenGateFired)

What am I doing wrong, and what do I need to do to accomplish my task?

Can you state the codes here? Use ` to do it.

1 Like

Use 3 backquotes at the starting of your code,
The paste ur code,
Then end it with 3 backquotes at the next line

Please reply this comment so I can continue.

1 Like

The OpenLaneGateEvent is both firing the close gate function and the open gate function simultaneously, with the CloseLaneGateEvent not being used at all on server side. Distribute both events each to their respective functions.

1 Like