Problems with team switching system

I am making a team switching system but I am having a lot of issues trying to get it to work.
If there is already a player in the team, it sends a request to join. Once the player joins the team, I try to join a different team but then it sends a request when it shouldn’t. If I try joining a different player’s team again, it joins without giving a request.

Video showing what happens:

I tried detecting how many players were in the team and checking if that player was the owner of the plot and if they were, it should send a request to join.

Server Script:

game.Players.PlayerAdded:Connect(function(player)
	local inteam = false
	for _, team in pairs(game.Teams:GetChildren()) do
		if inteam == false then
			if (#team:GetPlayers() == 0) then
				player.Team = team
				workspace.Plots:FindFirstChild(player.Team.Name).Owner.Value = player
				inteam = true
			end
		end
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	for _, plot in pairs(workspace.Plots:GetChildren()) do
		if plot.Owner.Value == player then
			plot.Owner.Value = nil
		end
	end
end)

game.ReplicatedStorage.Events.Server.RequestTeam.OnServerEvent:Connect(function(player, teamName)
	if teamName then
		if game.Teams:FindFirstChild(teamName) then
			if player.Team.Name ~= teamName then
				local plot = workspace.Plots:FindFirstChild(teamName)
				if plot then
					if plot.Owner.Value ~= nil and plot.Name == teamName then
						print("not nil")
						game.ReplicatedStorage.Events.Client.RequestGui:FireClient(plot.Owner.Value, player)
					elseif plot.Name == teamName and plot.Owner.Value == nil then
						player.Team = game.Teams:FindFirstChild(teamName)
						workspace.Plots:FindFirstChild(player.Team.Name).Owner.Value = player
						player:LoadCharacter()
					end
				end
			end
		end
	end
end)

game.ReplicatedStorage.Events.Server.JoinOtherPlayersTeam.OnServerEvent:Connect(function(player, requestingPlayer)
	local plot = workspace.Plots:FindFirstChild(player.Team.Name)
	if plot.Owner.Value == player then
		plot.Owner.Value = nil
		requestingPlayer.Team = player.Team
		requestingPlayer:LoadCharacter()
	end
end)

Client Script:

local debounce = false
local playerRequest

game.ReplicatedStorage.Events.Client.RequestGui.OnClientEvent:Connect(function(requestingPlayer)
	if debounce == false then
		debounce = true
		playerRequest = requestingPlayer
		script.Parent.Position = UDim2.new(0.5,0,-1,0)
		game:GetService("TweenService"):Create(script.Parent,TweenInfo.new(0.2,Enum.EasingStyle.Quad,Enum.EasingDirection.InOut,0,false,0),{Position = UDim2.new(0.5,0,0.025,0)}):Play()
	end
end)

script.Parent.No.Button.MouseButton1Click:Connect(function()
	game:GetService("TweenService"):Create(script.Parent,TweenInfo.new(0.2,Enum.EasingStyle.Quad,Enum.EasingDirection.InOut,0,false,0),{Position = UDim2.new(0.5,0,-1,0)}):Play()
	debounce = false
end)

script.Parent.Yes.Button.MouseButton1Click:Connect(function()
	if playerRequest then
		game.ReplicatedStorage.Events.Server.JoinOtherPlayersTeam:FireServer(playerRequest)
	end
	game:GetService("TweenService"):Create(script.Parent,TweenInfo.new(0.2,Enum.EasingStyle.Quad,Enum.EasingDirection.InOut,0,false,0),{Position = UDim2.new(0.5,0,-1,0)}):Play()
	debounce = false
end)
1 Like