Trying to make my players not be able to change teams so fast

I’m trying to make it so theres a cooldown between changing teams but it isn’t working and I have no idea. Here’s the GUI script:

local changeTeam = game.ReplicatedStorage.changeTeam

local Police = “Bright blue”
local Criminal = “Really red”

script.Parent.MouseButton1Click:Connect(function(player)
if _G.changedRecently == false then
changeTeam:FireServer(BrickColor.new(Police))
script.Parent.Parent.Visible = false
elseif _G.changedRecently == true then
script.Parent.Parent.Error.Visible = true
wait(5)
script.Parent.Parent.Error.Visible = false

end

end)

The gui script fires the event based on what people chose. Once it fires an event, it changes your team ussing the changeTeam script in serverscriptservice:

local players = game:GetService("Players")

_G.changedRecently = false

game.ReplicatedStorage.changeTeam.OnServerEvent:Connect(function(player, teamColor)
player.TeamColor = teamColor
player:LoadCharacter()
_G.changedRecently = true
wait(20)
_G.changedRecently = false
end)

but it’s not stopping me from changing teams instantly, any ideas?

local changeTeam = game.ReplicatedStorage.changeTeam
local Police = “Bright blue”
local Criminal = “Really red”

script.Parent.MouseButton1Click:Connect(function(player)
if _G.changedRecently == false then
changeTeam:FireServer(BrickColor.new(Police))
script.Parent.Parent.Visible = false
elseif _G.changedRecently == true then
script.Parent.Parent.Error.Visible = true
wait(5)
script.Parent.Parent.Error.Visible = false

end
end)

The gui script fires the event based on what people chose. Once it fires an event, it changes your team ussing the changeTeam script in serverscriptservice:

local players = game:GetService("Players")
_G.changedRecently = false

game.ReplicatedStorage.changeTeam.OnServerEvent:Connect(function(player, teamColor)
player.TeamColor = teamColor
player:LoadCharacter()
_G.changedRecently = true
wait(20)
_G.changedRecently = false
end)

The _G table that you’re using on the server is different from the _G table that each client has access to. The server and each client have their own global table that is completely separate from one another.

If you want to prevent the player from quickly changing their teams, you can do something like:

local debounces = {}

RemoteEvent.OnServerEvent:Connect(function(player, teamColor)
     if not debounces[player.Name] then
          debounces[player.Name] = true

          -- handle team changing

          wait(20)
          debounces[player.Name] = false
     end
end)